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?
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?
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.
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$$
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$$$$'));