粘连标记实际上就是配置 y,这个配置的作用为:匹配时完全按照正则对象中的 lastIndex 位置开始匹配,并且匹配的位置必须在 lastIndex 位置。
举个例子
js
const str = "Hello World!";
const reg = /W\w+/;//匹配以W开头的单词
console.log(reg.test(str));//true
//此处正常匹配
const reg2 = /W\w+/y;//加上粘连标志
console.log(reg2.test(str));//false
//加上粘连标记,从lastIndex位置开始匹配,初始为0,所以匹配不上
reg2.lastIndex = 6;
//修改lastIndex
console.log(reg2.test(str));//true
//可以匹配上