javascript - 为什么这个正则表达式输出不正确?

标签 javascript regex

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");

You can test it here .

关于javascript - 为什么这个正则表达式输出不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4545798/

相关文章:

javascript - 如何使用数据属性而不是它们的值链接两个下拉列表?

javascript - Firefox 开发人员控制台中的 XML 解析错误

regex - 如何在 python 正则表达式中获得所有可能的子组?

javascript - JS 正则表达式 : match list of phrases exactly (including hash symbols)

正则表达式在第一个管道后获取所有字符串

javascript - if (typeof window !== 'undefined' ) 的目的是什么

javascript - 自定义下载名称 <a href> 标签

javascript - 一个数组中的对象字面量和对象构造函数是否存在潜在问题?

python - 基于字符串模式删除列表中元素的最佳方法

javascript - RegEx 允许输入标签包含最多 255 个字符