Javascript String.replace with dynamic regular expressions?

前端 未结 4 1371
清酒与你
清酒与你 2020-12-30 22:50

I have the following code, which works, but I need to inject some different stuff into the regular expression object (regex2) at runtime. However, text.replace

4条回答
  •  误落风尘
    2020-12-30 23:34

    You can make a regular expression object from a string using the RegExp constructor function:

    var regExp = new RegExp(myString);  // regex pattern string
    
    text.replace(regExp, '');
    

提交回复
热议问题