myString.replace( VARIABLE, “”) … but globally

后端 未结 4 1264
栀梦
栀梦 2020-12-24 04:51

How can I use a variable to remove all instances of a substring from a string? (to remove, I\'m thinking the best way is to replace, with nothing, globally... right?)

<
4条回答
  •  醉话见心
    2020-12-24 05:41

    Well, you can use this:

    var reg = new RegExp(oldWord, "g");
    myString.replace(reg, "");
    

    or simply:

    myString.replace(new RegExp(oldWord, "g"), "");
    

提交回复
热议问题