正则新增特性
构造函数的变化 正则方法的扩展 u 修饰符 y 修饰符 s 修饰符
{
// es5
let regex = new RegExp("xyz", "i");
let regex2 = new RegExp(/xyz/i);
console.log(regex.test("xyz123"), regex2.test("xyz123")); // true true
// es6
let regex3 = new RegExp(/xyz/gi, "i");
console.log(regex3.flags); // i
// ES6为正则表达式新增了flags属性,会返回正则表达式的修饰符。
}
{
let s = "bbb_bb_b";
let a1 = /b+/g;
let a2 = /b+/y;
console.log("one", a1.exec(s), a2.exec(s));
// one ["bbb", index: 0, input: "bbb_bb_b", groups: undefined] ["bbb", index: 0, input: "bbb_bb_b", groups: undefined]
console.log("two", a1.exec(s), a2.exec(s));
// two ["bb", index: 4, input: "bbb_bb_b", groups: undefined] null
console.log(a1.sticky, a2.sticky); // false true
}
{
console.log("u-1", /^\uD83D/.test("\uD83D\uDC2A")); // u-1 true
console.log("u-2", /^\uD83D/u.test("\uD83D\uDC2A")); // u-2 false
console.log(/\u{61}/.test("a")); // false
console.log(/\u{61}/u.test("a")); // true
console.log(`\u{20BB7}`); // 𠮷
let s = "𠮷";
console.log("u-1", /^.$/.test(s)); // u-1 false
console.log("u-2", /^.$/u.test(s)); // u-2 true
console.log("test-1", /𠮷{2}/.test("𠮷𠮷")); // test-1 false
console.log("test-2", /𠮷{2}/u.test("𠮷𠮷")); // test-2 true
}