Javascript replace issue with $ [duplicate]

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.

Answers:

Answer

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?.

Answer

$$ 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 );
Answer

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.

Answer

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.

Answer

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.

Answer

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

Answer

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);

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.