19

I have a string like aman/gupta and I want to replace it to aman$$gupta and for that I am using JavaScript replace method as follows:

let a = "aman/gupta"
a = a.replace("/", "$")
console.log(a) // 'aman$gupta'

a = "aman/gupta"
a = a.replace("/", "$$")
console.log(a) // 'aman$gupta'

a = "aman/gupta"
a = a.replace("/", "$$$")
console.log(a) // 'aman$$gupta'

Why are the 1st and 2nd case identical and I get the expected result when I use $$$ instead of $$?

adiga
  • 34,372
  • 9
  • 61
  • 83
Aman Gupta
  • 3,627
  • 4
  • 40
  • 64
  • 9
    `$$` is a ["special value"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter) when used in a replacement string in [`.replace()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) – Andreas Aug 10 '16 at 06:48
  • Possible duplicate of [`string.replace` weird behavior when using dollar sign ($) as replacement](https://stackoverflow.com/q/9423722/1048572) – Bergi Jan 16 '23 at 22:43

6 Answers6

29

It’s because $$ inserts a literal "$".

So, you need to use:

a = "aman/gupta";
a = a.replace("/", "$$$$"); // "aman$$gupta"

See the following special 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 non-negative integer less than 100, inserts the _n_th parenthesized submatch string, provided the first argument was a RegExp object.
$<Name> Where Name is a capturing group name. If the group is not in the match, or not in the regular expression, or if a string was passed as the first argument to replace instead of a regular expression, this resolves to a literal (e.g., "$<Name>").
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
12

To avoid the need to escape special characters you can use anonymous function as a replacer

a = "aman/gupta";
a = a.replace("/", function() {return "$$"});
console.log(a); // "aman$$gupta"

String.prototype.replace() documentation

Specifying a function as a parameter

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. (Note: the above-mentioned special replacement patterns do not apply in this case.) Note that the function will be invoked multiple times for each full match to be replaced if the regular expression in the first parameter is global.

Community
  • 1
  • 1
edo888
  • 398
  • 2
  • 12
8

Also you can use split and join for better performance and $ isn't special for those functions.

var a = "aman/gupta"
a = a.split('/').join('$$')
alert(a); // "aman$$gupta"
Sojtin
  • 2,714
  • 2
  • 18
  • 32
0

The replace method provides replacement patterns that start with a dollar sign. One of them is $$ which inserts a single $. A single dollar sign in the replacement string will result in a literal one.

So if you want clean literal dollar signs, use $$ replacement patterns accordingly:

console.log('aman/gupta'.replace('/','$$')); // aman$gupta
console.log('aman/gupta'.replace('/','$$$$')); // aman$$gupta
console.log('aman/gupta'.replace('/','$$$$$$')); // aman$$$gupta
dakab
  • 5,379
  • 9
  • 43
  • 67
-1

In regular expression replace with groups, if replacement is a variable, it needs to dollar sign escaped. Otherwise there will be bugs.

function escapeDollarSign(str) {
return str.replace(/\$/g, "$$$$")
}
-2

Use below code its working for me.

var dollar = "$$$$";
console.log('abhishe/kadam'.replace('/', dollar.replace(new RegExp('\\$', 'g'), '$$$')));
4b0
  • 21,981
  • 30
  • 95
  • 142