I have a string of "aa$bb$cc"; and I want to replace it to be "aa\$bb\$cc"
I’ve tried this:
var str = "aa$bb$cc"
str.replace(/$/g, "\\$");
But the outpus is "aa$bb$cc\$"
How can I replace it and get same (correct…) result in all browsers?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
$in a regular expression means “end of the line”. Therefore, it’s replacing all the ends of lines with\$. Escape the$in the regular expression and it works.