使用非捕获分组来标记无关部分,使其不在匹配结果中显示。例如,将正则表达式/(a(?:bc)?)/
中的(?:bc)
使用非捕获分组标记,忽略掉无关部分,仅匹配到ab
或abc
。
示例代码:
const regex = /(a(?:bc)?)/;
const str = 'This is abc and ab.';
const match = str.match(regex);
console.log(match); // ['ab', 'abc']
上一篇:避免绘制冗余像素