0

Consider the code below:

var my_string = "aicId";
var my_pattern = "i";
var my_regex = new RegExp(my_pattern,"gi");
var my_result = my_string.replace(my_regex,"x$&y");
alert (my_result);

This would return:

axiycxIyd

Now, what if I want it to return this:

ax$&ycx$&yd

What do I need to do?

I tried to escape them like this:

var my_result = my_string.replace(my_regex,"x\$\&y");

or this:

var my_result = my_string.replace(my_regex,"x\\$&y");

or this:

var my_result = my_string.replace(my_regex,"x$&y");

or this:

var dollar = '$';
var amper = '&';
var my_result = my_string.replace(my_regex,"x" + dollar.charCodeAt(0) + amper.charCodeAt(0) + "y");

What else can I try?

Dentra Andres
  • 371
  • 1
  • 7
  • 18
  • *What else can I try?* I don't know, what about reading the documentation, or searching on SO? –  Mar 11 '16 at 19:00
  • Thank you for nothing. You don't need to be rude. I spent a good two hours reading the documentation and searching the web, including SO. Interesting that you don't have the time to help, but have the time to be... Thankfully, someone else was more generous. – Dentra Andres Mar 11 '16 at 19:39
  • SO has nothing to do with generosity. If you want a help forum where people are generously helping other people who can't be bothered to do research or read the docs, look elsewhere. SO is about interesting, well-researched questions, the answers to which form a useful body of knowledge about programming. If you really spent two hours researching this, then you seriously need to beef up your researching skills, if only for the sake of your future development as a programming professional. The answer to your question is right on the `String#replace` MDN page, near the top, in plain sight. –  Mar 12 '16 at 10:27

2 Answers2

0

For inserting a $ you need $$, in your case $& is special replacement pattern, if you escape first $ by $$ it will work.

var my_string = "aicId";
var my_pattern = "i";
var my_regex = new RegExp(my_pattern, "gi");
var my_result = my_string.replace(my_regex, "x$$&y");
document.write('<pre>' + JSON.stringify(my_result, 0, 2) + '</pre>');

Or you can use function as second parameter without escaping anything:

From MDN:

You can specify a function as the second parameter. In this case, the function will be invoked after the match has been performed. The function's result (return value) will be used as the replacement string.

var my_string = "aicId";
var my_pattern = "i";
var my_regex = new RegExp(my_pattern, "gi");
var my_result = my_string.replace(my_regex, function() {
    return "x$&y";
});
document.write('<pre>' + JSON.stringify(my_result, 0, 2) + '</pre>');
isvforall
  • 8,768
  • 6
  • 35
  • 50
-1

If you want to return "ax$&ycx$&yd", this should work:

var my_result = my_string.replace(my_regex,"x$$\&y");

Because in the first part of the regex, "$" needs to be escaped, because it's a special regex character denoting the end of the string.

The second part of the regex is considered as a "normal" string, where "$" doesn't have a special meaning.

From here.

Community
  • 1
  • 1
Matt
  • 63
  • 2
  • 12