node.js string.replace doesn't work?

前端 未结 4 1858
庸人自扰
庸人自扰 2021-02-02 05:12
var variableABC = \"A B C\"; 
variableABC.replace(\'B\', \'D\') //wanted output: \'A D C\'

but \'variableABC\' didn\'t change :

4条回答
  •  星月不相逢
    2021-02-02 06:00

    If you just want to clobber all of the instances of a substring out of a string without using regex you can using:

        var replacestring = "A B B C D"
        const oldstring = "B";
        const newstring = "E";
        while (replacestring.indexOf(oldstring) > -1) {
            replacestring = replacestring.replace(oldstring, newstring);
        }        
        //result: "A E E C D"
    

提交回复
热议问题