var re = /apples/gi;
var str = "Apples are round, and apples are juicy.";
var newstr = str.replace("apples", "oranges","gi");
document.write(newstr);
它应该输出 oranges are round, and oranges are juicy.
,因为不区分大小写,而是输出 Apples are round, and oranges are juicy.
为什么??
最佳答案
没有 .replace()
使用该签名的方法,而是使用您创建的正则表达式,如下所示:
var re = /apples/gi;
var str = "Apples are round, and apples are juicy.";
var newstr = str.replace(re, "oranges");
关于javascript - 为什么这个正则表达式输出不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4545798/