4

I am trying to replace "this" in the below example with "$$Ashok".I am not getting expected output.

    var adHtmltext ="this is ashok"
    adHtmltext = adHtmltext.replace("this", "$$Ashok");
    alert(adHtmltext );

why it is showing one $ in output? how to fix this?

Here is the jsfiddle http://jsfiddle.net/RxDa5/

Please help.

Java P
  • 2,241
  • 6
  • 31
  • 45

7 Answers7

6

Have a look at the MDN documentation:

The replacement string can include the following special replacement patterns:

$$ Inserts a "$".

So you have to do:

adHtmltext.replace("this", "$$$$Ashok");

See also Javascript string replace weirdness -- $$$$ gets collapsed to $$ -- what's the reason behind this result?.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
3

$$ is the escape code for $, since $ is the escape code for a regex backreference. Unfortunately, you need this:

var adHtmltext ="this is ashok"
adHtmltext = adHtmltext.replace("this", "$$$$Ashok");
alert(adHtmltext );
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
3

The dollar sign is a reserved character for .replace()

Indeed, in your jsFiddle code, right at the top, you've used it for it's reserved purpose -- ie the $1 that you've got in there to capture part of the expression.

$$ is used to escape a dollar sign. You need two dollar signs in this context for every single dollar sign you actually want.

This is because otherwise you couldn't have the string $1 in your output.

SDC
  • 14,192
  • 2
  • 35
  • 48
2

The .replace method will also accept regular expressions as the first argument, and if you group a portion of the text, you can include it in your output text with a "back-reference" using the '$' character and a number specifying which group to use ($1, $2, etc).

Because the '$' has a special meaning in this context, it needs to be escaped, and '$$' is the escape sequence to produce a normal '$', so you just need '$$$$Ashok' in your code.

1

There are special patterns that can be included in the string that you replace the target pattern with, and a string with '$$' is one of them. See the Mozilla MDN docs for a better reference.

In your case specifically, '$$' becomes '$' as certain combinations of other characters with '$', like '$&' are reserved for matching with certain substrings. If you want your replacement to work, just use '$$$$Ashok', which will become '$$Ashok' in the final string.

Clarence Leung
  • 2,446
  • 21
  • 24
1

Any custom replacer function can solve this problem more elegantly. You just have to return the intended string from it and it will be replaced as it is.

function customReplacer() {
return "$$Ashok";
}
adHtmltext = adHtmltext.replace("this", customReplacer);
Sudhir K
  • 37
  • 1
  • 7
  • While clever, this is not very extensible. What if I have varying replacement strings I want to use, depending on the input string? – random_user_name Feb 08 '19 at 18:02
  • @cale_b, This custom replacer function takes few parameters, I think we can make use of them. [Link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter) – Sudhir K Feb 10 '19 at 16:10
0

Looking for a generic solution, I obtained the following:

var input = prompt( 'Enter input:' ) || '';
var result = 'foo X bar X baz'.replace( /X/g, input.replace( /\$/g, '$$$$' ) );

It works:

input: $$
result: foo $$ bar $$ baz

input: $&
result: foo $& bar $& baz

But it's a bit tricky, because of the multi-level $ escaping. See that $$$$ in the inner replace...


So, I tried using a callback, to which special replacement patterns aren't applied:

var result = 'foo X bar X baz'.replace( /X/g, function () {
    var input = prompt( 'Enter input:' ) || '';
    return input;
} );

It works too, but has a caveat: the callback is executed for each replacement. So in the above example, the user is prompted twice...

Finally, here is the fixed code for the "callback" solution, by moving the prompt out of the replace callback:

var input = prompt( 'Enter input:' ) || '';
var result = 'foo X bar X baz'.replace( /X/g, function () {
    return input;
} );

To summarize, you have two solutions:

  • Apply a .replace(/\$/g, '$$$$') escaping on the replacement string
  • Use a callback, which does nothing more than just returning the replacement string

MDN reference: String.prototype.replace()#Description

Gras Double
  • 15,901
  • 8
  • 56
  • 54