JavaScript RegExp隐含变量

时间:2018-07-12 07:56:08

标签: javascript regex string replace

尝试不考虑大小写而替换字符串中的所有a。

str = "All a's will be replaced";
str.replace(/a/gi, 'Aa');

如预期的那样,以上代码会产生Aall Aa's will be replAaced

但是

regExp = new RegExp('/a/gi');
str.replace(regExp, 'Aa');

以上使用RegExp模式的行会产生原始字符串All a's will be replaced

如何达到预期效果?

0 个答案:

没有答案
相关问题