Replace Javascript Regexp Matched Group With A Dollar Sign
This one should be pretty simple : Let's take the string : str='1.99 or 4.89' I want to add a dollar sign in front of the amounts. I tried : str.replace(/(\d\.\d\d)/g,'$$1')); it
Solution 1:
"1.99 or 4.89".replace(/(\d\.\d\d)/g, "$$$1")
// => "$1.99 or $4.89"
Since $ is special in replacement string, it must be escaped into $$ for a literal $. It is not escaped using the \ character, which is a general string escape mechanism, and processed before the string reaches replace (i.e. if you say "\$", it becomes "$" before being passed as an argument, so replace never sees the escaping).
Post a Comment for "Replace Javascript Regexp Matched Group With A Dollar Sign"