两种创建正则的区别:
- 1.字面量
- 2.new RegExp();
区别:
1.字面量的方式不可以使用字符串拼接 new的方式可以
2.new的方式,如果需要用到\,需要使用\\去替代。
列子!
// 第一个点区别
var c = "d"
var a = /a+c+bc/;
console.log(typeof a);//object
var b = new RegExp("a"+c+"bc");
console.log(typeof b);
var str = "123adbc";
console.log(a.test(str))
console.log(b.test(str));
// 第二点区别
var a = /^\d{3}$/;
var b = new RegExp("^\\d{3}$");
var c = "123";
console.log(a);
console.log(b);
console.log(a.test(c));
console.log(b.test("ddd"));
913

被折叠的 条评论
为什么被折叠?



