正则表达式分组(Grouping)

一 捕获型

(x)  匹配 x ,并且捕获匹配项

const regExp = /(\w+)\s+(\d+)/;
const str = 'Android 8';
str.replace(regExp,'$1,KFC')

二 非捕获型

(?:x)  匹配 x ,但不捕获匹配项

const regExp = /(?:\w+)\s+(\d+)/;
const str = 'Android 8';
str.replace(regExp,'KFC')

猜你喜欢

转载自www.cnblogs.com/sea-breeze/p/11862882.html