4

Let's say I have:

<h1>Win $50<h1>

I need to change the $ sign to € with jQuery
So I can do it like that:

$('h1').text( $('h1').text().replace('$', '€') );

But what if I have something like that:

<h1>Win $50 and Get $100</h1>

It only change the first one, How can I change it all?

3 Answers 3

9

Use regexp with g flag

$('h1').text( $('h1').text().replace(/\$/g, '€') );
Sign up to request clarification or add additional context in comments.

2 Comments

I answered the same thing 2 minutes earlier.
how can one use this with a variable, let's say the search in replace is user input?
5

With a regex.

txt = $('h1').text().replace(/\$/g, '€')
$('h1').text(txt);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Win $50 and get $100</h1>

g stands for "global" (all instances)

As RafH said, $ is a special character in regex, so it needs to be escaped with \.

1 Comment

$ needs to be escaped
2

Here's a helper function that's a part of my utility library that I add into every piece of code that I write. Feel free to use it any way you like.

String.prototype.replaceAll = function (stringFind, stringReplace) {
    var ex = new RegExp(stringFind.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"), "g");
    return this.replace(ex, stringReplace);
};

It adds a replaceAll function to all strings so you don't have to constantly use RegEx throughout your code.

You can use it like this

str.replaceAll("$", "€");

Here's an example:

String.prototype.replaceAll = function (stringFind, stringReplace) {
  var ex = new RegExp(stringFind.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"), "g");
  return this.replace(ex, stringReplace);
};

var str = "Win $50 and Get $100";

document.body.innerHTML = str.replaceAll("$", "€");


EDIT Test cases per comment below:

String.prototype.replaceAll = function (stringFind, stringReplace) {
  var ex = new RegExp(stringFind.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"), "g");
  return this.replace(ex, stringReplace);
};

var str = "test test abc test abc $%& test $%& $%&. test. abc $%& . .";
document.body.innerHTML = str  + "<br />";
document.body.innerHTML += str.replaceAll(".", "") + "<br />";
document.body.innerHTML += str.replaceAll("%&", "") + "<br />";
document.body.innerHTML += str.replaceAll("test", "") + "<br />";
document.body.innerHTML += str.replaceAll("a-z", "") + "<br />";


Update

Per the comments from Danilo below (Thank you!), the original function would not allow for certain special characters, and would misbehave if a text range was entered into the replaceAll() function. In my own code, I had not come across these cases as of yet.

Per this answer, I have updated my function so that it now escapes the RegEx to be more efficient in the provided test case scenarios.

4 Comments

you are using square brackets withing your regexp: if stringFind has more than one charactore or if stringFind = "." you will get unexpected results.
@Danilo - Please see my edit with test cases and explain your point in greater detail. This function works just fine with "." or multiple characters as shown in the snippet above.
you are indeed right: the dot gets handled correctly, however words no not, try adding this to your example: document.body.innerHTML += str.replaceAll("test", "trial") + "<br />"; document.body.innerHTML += str.replaceAll("a-z", "someText") + "<br />";
Thanks @Danilo - I appreciate you pointing this out. I have updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.