gpt4 book ai didi

javascript - 为什么这个简单的 javascript 正则表达式在第二种形式上不匹配?

转载 作者:行者123 更新时间:2023-11-30 17:28:01 26 4
gpt4 key购买 nike

我正在学习 javascript 中的正则表达式,我得到了一个有趣的结果

console.log("this will be true");
console.log(/\d+/.test("0"));
var hasNumberFormat = new RegExp("\d+");
console.log("this will be false");
console.log(hasNumberFormat.test("0"));

这输出到:

this will be true 
true
this will be false
false

知道为什么会这样吗?

谢谢!

最佳答案

我认为你需要像这样转义“\”:

var hasNumberFormat = new RegExp("\\d+");

关于javascript - 为什么这个简单的 javascript 正则表达式在第二种形式上不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23835580/

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