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

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

    There are already multiple answers using str.replace() (which is fair enough for this question) and regex but you can use combination of str.split() and join() together which is faster than str.replace() and regex.

    Below is working example:

    var text = "this is some sample text that i want to replace";
    
    console.log(text.split("want").join("dont want"));

提交回复
热议问题