gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:46:54 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com