2

let str = '$$double_dollars$$'
console.log(str.replace('$$double_dollars$$', '$$no_double_dollars$$'));
// => $no_double_dollars$
// expected $$no_double_dollars$$

Why is this happening? How to work around this bug?

yunzen
  • 32,854
  • 11
  • 73
  • 106
John Lenson
  • 167
  • 2
  • 9
  • 1
    I'm pretty sure str.replace is interpreting $ has a Regular expression special character ($ means the string must begin with what's ahead of $) – H. Figueiredo Mar 14 '19 at 11:49
  • 1
    Read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace – noetix Mar 14 '19 at 11:50
  • Working around this could be escaping the $ sign (making sure it isn't interpreted as a special character), something like "\$\$double_dollars\$\$" – H. Figueiredo Mar 14 '19 at 11:50
  • `str.replace('$$double_dollars$$', '$$$$still_double_dollars$$$$');` – Jeto Mar 14 '19 at 11:52
  • @H.Figueiredo Yes, `$` is a RegExp special character, but there's no RegExp in the question. – Teemu Mar 14 '19 at 11:56
  • @Teemu String.replace will use a RegExp if provided, so yeah if Javascript considers: replace(something) that "something" is a RegExp, then there will be RegExp in the question. – H. Figueiredo Mar 14 '19 at 12:00
  • @H.Figueiredo JavaScript doesn't consider that "something" being a RegExp, if a string was provided. It's a string, and it is not implicitly converted to a RegExp. ["_\[A string\] is treated as a verbatim string and is not interpreted as a regular expression_"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Parameters). – Teemu Mar 14 '19 at 13:23
  • @Teemu you're correct! I was thinking that Javascript was interpreting the string as RegExp literal, my bad. (I would still consider this behaviour similar to RegExp, given $ is selecting some part of the match) – H. Figueiredo Mar 14 '19 at 13:44

3 Answers3

2

According to the MDN docs for String.prototype.replace (as what @Alex states), there is a list of special patterns which are evaluated accordingly and one of them is what you're using.

The special pattern is as follows:

  • $$ inserts a $

See the MDN docs for the full list of special patterns.

And as what @H.Figueiredo has commented, you can escape the dollar signs or follow one of the answers posted seconds after this answer.

Edric
  • 24,639
  • 13
  • 81
  • 91
2

See: MDN - String.prototype.replace # Specifying a function as a parameter

The replacement string can include the following special replacement patterns:

Pattern   Inserts  
$$        Inserts a "$".  
$&        Inserts the matched substring.  
$`        Inserts the portion of the string that precedes the matched substring.  
$'        Inserts the portion of the string that follows the matched substring.  
$n        Where n is a positive integer less than 100, inserts the nth parenthesized  
          submatch string, provided the first argument was a RegExp object. 
          Note that this is 1-indexed.

let str = '$$double_dollars$$'
console.log(str.replace('$$double_dollars$$', '$$$$yes_double_dollars$$$$'));
// => $$yes_double_dollars$$
yunzen
  • 32,854
  • 11
  • 73
  • 106
0

See the documentation for passing a replacement String to replace.

$ is a special character. To express a literal $, use $$.

let str = '$$double_dollars$$'
console.log(str.replace('$$double_dollars$$', '$$$$result$$$$'));
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335