How can I perform a str_replace in JavaScript, replacing text in JavaScript?

后端 未结 22 1888
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 00:29

I want to use str_replace or its similar alternative to replace some text in JavaScript.

var text = \"this is some sample text that i want to re         


        
22条回答
  •  青春惊慌失措
    2020-12-01 00:59

    You can use

    text.replace('old', 'new')
    

    And to change multiple values in one string at once, for example to change # to string v and _ to string w:

    text.replace(/#|_/g,function(match) {return (match=="#")? v: w;});
    

提交回复
热议问题