为什么这个简单的javascript正则表达式不是';在第二张表格上不匹配吗?

为什么这个简单的javascript正则表达式不是';在第二张表格上不匹配吗?,javascript,regex,Javascript,Regex,我正在学习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 th

我正在学习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+");
改为

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

对象将字符串作为参数。

d'oh!当然,它读一个字符串,这现在非常有意义