Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript replace issue with $ [duplicate]

Tags:

javascript

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.

like image 202
Java P Avatar asked Dec 07 '22 13:12

Java P


1 Answers

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

like image 184
Felix Kling Avatar answered Dec 27 '22 13:12

Felix Kling