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

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

    If you don't want to use regex then you can use this function which will replace all in a string

    Source Code:

    function ReplaceAll(mystring, search_word, replace_with) 
    {
        while (mystring.includes(search_word))
        {
            mystring = mystring.replace(search_word, replace_with);
        }
    
        return mystring;  
    }
    

    How to use:

    var mystring = ReplaceAll("Test Test", "Test", "Hello"); 
    

提交回复
热议问题