彻底理解 JavaScript 中的正则表达式:exec、match、matchAll 和 test 功能解析
最编程
2024-02-11 14:51:52
...
let myRe = /d(b+)(c*)d/g;
let str = 'cdbbcdbsbzdbd';
myRe.test(str);
// 第 1 次执行结果 => true
// 执行完后 myRe.lastIndex 为 6,下一次 test 将从原始字符串下标 6 开始检索
myRe.test(str);
// 第 2 次执行结果 => true
// 执行完后 myRe.lastIndex 为 13,下一次 test 将从原始字符串下标 13 开始检索
myRe.test(str);
// 第 3 次执行 => false
// 执行完后 myRe.lastIndex 为 0,下一次 test 将从原始字符串下标 0 开始检索
myRe.test(str);
// 第 4 次执行结果 => true
// 执行完后 myRe.lastIndex 为 6
let myRe2 = /d(b+)(c*)d/; // myRe2 不携带 g 标志
myRe2.test(str);
// 第 1 次执行结果 => true
// 执行完后 myRe.lastIndex 为 0,下一次 test 将从原始字符串下标 0 开始检索
myRe2.test(str);
// 第 2 次执行结果 => true
// 执行完后 myRe.lastIndex 为 0,下一次 test 将从原始字符串下标 0 开始检索
myRe2.test(str);
// 第 3 次执行 => true
// 执行完后 myRe.lastIndex 为 0,下一次 test 将从原始字符串下标 0 开始检索