gpt4 book ai didi

Javascript 替换函数不适用于正则表达式

转载 作者:行者123 更新时间:2023-11-30 07:02:45 24 4
gpt4 key购买 nike

为什么这段代码不能正常工作?

var temp = "@TEMP (A1)"
var text = "1st Oct @TEMP (A1)"
text = text.replace(new RegExp(temp, "gi"), "");
console.log(text); //I get same text even though I used replace instead of 1st Oct??

谁能解释这里出了什么问题?

最佳答案

您需要引用temp 的特殊字符,它被直接用作您的正则表达式。 () 字符将字符分组到模式中,而不是实际匹配“(”和“)”。

关于Javascript 替换函数不适用于正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12694368/

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