Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  122] [ 5]  / answers: 1 / hits: 23113  / 15 Years ago, fri, march 12, 2010, 12:00:00

As the title states, I need to relace all occurrences of the $ sign in a string variable with an underscore.



I have tried:



str.replace(new RegExp('$', 'g'), '_');


But this doesn't work for me and nothing gets replaced.


More From » replace

 Answers
76

The $ in RegExp is a special character, so you need to escape it with backslash.



new_str = str.replace(new RegExp('\$', 'g'), '_');


however, in JS you can use the simpler syntax



new_str = str.replace(/$/g, '_');

[#97356] Tuesday, March 9, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ibrahimgilbertoz

Total Points: 420
Total Questions: 112
Total Answers: 92

Location: Bonaire
Member since Sat, May 1, 2021
3 Years ago
;