0

Javascript code:

var testStr = "this is inside the {replace} expression";
var test2Str = "second $ short";
var test3Str = "third '$' short";
var test4Str = "third '$ ' short";
var test5Str = "third $' short";
var test6Str = "third '$$' short";
var test7Str = testStr.replace('{replace}', test2Str);
var test8Str = testStr.replace('{replace}', test3Str);
var test9Str = testStr.replace('{replace}', test4Str);
var test10Str = testStr.replace('{replace}', test5Str);
var test11Str = testStr.replace('{replace}', test6Str);

console.log("test2Str: " + test2Str);
console.log("test3Str: " + test3Str);
console.log("test4Str: " + test4Str);

console.log("test5Str: " + test5Str);
console.log("test6Str: " + test6Str);
console.log("test7Str: " + test7Str);
console.log("test8Str: " + test8Str);
console.log("test9Str: " + test9Str);
console.log("test10Str: " + test10Str);
console.log("test11Str: " + test11Str);

produces:

test2Str: second $ short
test3Str: third '$' short
test4Str: third '$ ' short
test5Str: third $' short
test6Str: third '$$' short
test7Str: this is inside the second $ short expression
test8Str: this is inside the third ' expression short expression
test9Str: this is inside the third '$ ' short expression
test10Str: this is inside the third  expression short expression
test11Str: this is inside the third '$' short expression

for some reason the '$ causes the replace function to break the replacement string into two parts: one that is replaced in the correct location and the latter part at the end of the entire string. This behavior is the same in Chrome and in node v10.16.2 command line. Any ideas? Are there any other escape characters that could cause similar problems?

Blaine
  • 1
  • 2
  • Welcome to Stack Overflow! Please have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Please [**search**](/search?q=%5Bjs%5D+replace+dollar) before posting. More about searching [here](/help/searching). – T.J. Crowder Mar 01 '20 at 14:08

1 Answers1

-1

The .replace() method sees $ in the replacement string as a metacharacter, used to indicate a capturing group value from the regular expression. If you want a simple $, the replacement string should include $$.

More details here on MDN.

Pointy
  • 371,531
  • 55
  • 528
  • 584
  • Pointy, thanks so much for the quick response! OK, so this seems like odd behavior, and possibly inconsistent. For the above examples I listed: (1) the `$` inserts a `$` instead of what the documentation states. (2) `$'` says it should insert the portion of the string that follows the matched substring, but it turns out that splits the argument string, and duplicates the rest of the target string before and after the argument. is that explained in the documentation? (3) it seems like it would be a good idea to have an argument that does the replacement without these special patterns listed? – Blaine Mar 02 '20 at 00:40
  • It's usually a good idea to conclude that the way something like JavaScript works is correct and intended, even if surprising, given that it's the basis of pretty much the entire Internet. – Pointy Mar 02 '20 at 02:08
  • um no. a much better answer was just posted on the bottom of this similar question: https://stackoverflow.com/questions/9423722/string-replace-weird-behavior-when-using-dollar-sign-as-replacement. Basically, use the function() argument for the replacement string. Its in the docs. nice API, its perfect. – Blaine Mar 03 '20 at 01:11