JS 中正则的/g 问题
let reg = /h/g;
reg.test("hh"); // true
reg.test("hh"); // true
reg.test("hh"); // false
g 是全局匹配,每次匹配都会从上一次匹配的位置开始,所以第三次的时候就是 false 了。
本文解释了 JavaScript 正则表达式中 /g 全局匹配修饰符的行为,以及它如何影响 `test()` 方法的多次调用。
let reg = /h/g;
reg.test("hh"); // true
reg.test("hh"); // true
reg.test("hh"); // false
g 是全局匹配,每次匹配都会从上一次匹配的位置开始,所以第三次的时候就是 false 了。