How can I replace all occurrences of a dollar ($) with an underscore (_) in javascript?

后端 未结 4 1970
傲寒
傲寒 2021-01-03 19:29

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(\'$         


        
4条回答
  •  天命终不由人
    2021-01-03 20:09

    ........

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

    Becaue $ is special char in js, you need to escape it.

提交回复
热议问题