{ //es5中 let regex = new RegExp('xyz','i'); let regex2 = new RegExp(/xyz/i); console.log(regex.test('xyz123'),regex2.test('xyz123')); // true,true //e ...
{
//es5中
let regex = new RegExp('xyz', 'i');
let regex2 = new RegExp(/xyz/i);
console.log(regex.test('xyz123'), regex2.test('xyz123')); // true,true
//es6中,flags是新增的,獲取正則對象修飾符
let regex3 = new RegExp(/xyz/ig, 'i');
console.log(regex3.flags); // i 後一個覆蓋前面的
}
//es5中常見修飾符是g、i;es6中新增 y,u
//exec() 方法用於檢索字元串中的正則表達式的匹配。
{
let s = 'bbb_bb_b';
let a1 = /b+/g; //g忽略_從上一次匹配的位置繼續尋找
let a2 = /b+/y; //y不忽略。匹配不上
console.log('one', a1.exec(s), a2.exec(s)); //bbb bbb
console.log('two', a1.exec(s), a2.exec(s)); //bb null
console.log('three', a1.exec(s), a2.exec(s)); //b bbb
//新增sticky,判斷是否又y修飾符
console.log(a1.sticky, a2.sticky); //false true
} {
console.log('u-1', /^\uD83D/.test('\uD83D\uDC2A')); //true 分隔了\uD83D 和 \uDC2A
console.log('u-1', /^\uD83D/u.test('\uD83D\uDC2A')); //false 把\uD83D\uDC2A 當成一體了
console.log(/\u{61}/.test('a')); //false
console.log(/\u{61}/u.test('a'));//true 加上u才能被識別
console.log(`\u{20BB7}`); //