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

后端 未结 22 1851
没有蜡笔的小新
没有蜡笔的小新 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:58

    Use JS String.prototype.replace first argument should be Regex pattern or String and Second argument should be a String or function.

    str.replace(regexp|substr, newSubStr|function);

    Ex:

    var str = 'this is some sample text that i want to replace'; var newstr = str.replace(/want/i, "dont't want"); document.write(newstr); // this is some sample text that i don't want to replace

提交回复
热议问题