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

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

    Added a method replace_in_javascript which will satisfy your requirement. Also found that you are writing a string "new_text" in document.write() which is supposed to refer to a variable new_text.

    let replace_in_javascript= (replaceble, replaceTo, text) => {
      return text.replace(replaceble, replaceTo)
    }
    
    var text = "this is some sample text that i want to replace";
    var new_text = replace_in_javascript("want", "dont want", text);
    document.write(new_text);

提交回复
热议问题