捕获组

用小括号包裹的部分叫做捕获组,捕获组会出现在匹配结果中

var reg = /(\d[a-z])([a-z]+)/g;
var s = "2afsdf-5fdgdfg-9asddf";
while (result = reg.exec(s)) {
    console.log(result);
}

// [
//     '2afsdf', // 大正则的匹配结果
//     '2a',     // 第一个捕获组(第一个括号)
//     'fsdf',   // 第二个捕获组(第二个括号)
//     index: 0,
//     input: '2afsdf-5fdgdfg-9asddf',
//     groups: undefined
// ]
// [
//     '5fdgdfg', // 大正则的匹配结果
//     '5f',      // 第一个捕获组(第一个括号)
//     'dgdfg',   // 第二个捕获组(第二个括号)
//     index: 7,
//     input: '2afsdf-5fdgdfg-9asddf',
//     groups: undefined
// ]
// [
//     '9asddf', // 大正则的匹配结果
//     '9a',     // 第一个捕获组(第一个括号)
//     'sddf',   // 第二个捕获组(第二个括号)
//     index: 15,
//     input: '2afsdf-5fdgdfg-9asddf',
//     groups: undefined
// ]

具名捕获组

捕获组可以命名,叫做具名捕获组 (?<命名>pattern),最后都放入 结果的 groups

// 得到每个日期的 年、月、日
var s = "2015-5-1, 2019-6-19, 2000-04-28";
var reg = /(?<year>\d{4})-(?<month>\d{1,2})-(?<day>\d{1,2})/g;
while (result = reg.exec(s)) {
    console.log(result.groups);
}

// { year: '2015', month: '5', day: '1' }
// { year: '2019', month: '6', day: '19' }
// { year: '2000', month: '04', day: '28' }

非捕获组

非捕获组 (?:pattern) 只匹配,不捕获,把 () 当成一个整体来匹配

// 匹配年月日,只捕获 日
var s = "2015-5-1, 2019-6-19, 2000-04-28";
var reg = /(?:\d{4})-(?:\d{1,2})-(\d{1,2})/g;
while (result = reg.exec(s)) {
    console.log(result);
}

// [
//     '2015-5-1',
//     '1',         // 只捕获 日
//     index: 0,
//     input: '2015-5-1, 2019-6-19, 2000-04-28',
//     groups: undefined
// ]
// [
//     '2019-6-19',
//     '19',         // 只捕获 日
//     index: 10,
//     input: '2015-5-1, 2019-6-19, 2000-04-28',
//     groups: undefined
// ]
// [
//     '2000-04-28',
//     '28',         // 只捕获 日
//     index: 21,
//     input: '2015-5-1, 2019-6-19, 2000-04-28',
//     groups: undefined
// ]

反向引用

  1. 在正则表达式中,利用 \捕获组编号 反向引用之前的 捕获组

  2. eg: 捕获和 分组 1 重复 { 1, }

// 找出该字符串中连续的字符

var s = "aaaaaaaabbbbbbbbbccccccdefgggggggg";
var reg = /(\w)\1+/g;

while (result = reg.exec(s)) {
    console.log(result);
}

// [
//     'aaaaaaaa',
//     'a',         // 捕获结果
//     index: 0,
//     input: 'aaaaaaaabbbbbbbbbccccccdefgggggggg',
//     groups: undefined
// ]
// [
//     'bbbbbbbbb',
//     'b',         // 捕获结果
//     index: 8,
//     input: 'aaaaaaaabbbbbbbbbccccccdefgggggggg',
//     groups: undefined
// ]
// [
//     'cccccc',
//     'c',         // 捕获结果
//     index: 17,
//     input: 'aaaaaaaabbbbbbbbbccccccdefgggggggg',
//     groups: undefined
// ]
// [
//     'gggggggg',
//     'g',         // 捕获结果
//     index: 26,
//     input: 'aaaaaaaabbbbbbbbbccccccdefgggggggg',
//     groups: undefined
// ]

正向断言(预查)

(?=pattern) 检查某个字符后面的字符是否满足某个规则,该规则不成为匹配结果,并且不称为捕获组

// 获取字符后面紧跟着数字的字符,qwe1 => e

var s = "sdfsdf3434343sdfsa545454dfsdfsfsd6754";
var reg = /[a-zA-Z](?=\d+)/g;
while (result = reg.exec(s)) {
    console.log(result);
}
// [
//     'f',
//     index: 5,
//     input: 'sdfsdf3434343sdfsa545454dfsdfsfsd6754',
//     groups: undefined
// ]
// [
//     'a',
//     index: 17,
//     input: 'sdfsdf3434343sdfsa545454dfsdfsfsd6754',
//     groups: undefined
// ]
// [
//     'd',
//     index: 32,
//     input: 'sdfsdf3434343sdfsa545454dfsdfsfsd6754',
//     groups: undefined
// ]

负向断言(预查)

(?!pattern) 检查某个字符后面的字符是否不满足某个规则,该规则不成为匹配结果,并且不称为捕获组

// 获取字符后面没有紧跟着数字的字符,qwe1 => q、w

var s = "afg43223444wr423424243";
var reg = /[a-zA-Z](?!\d+)/g;
while (result = reg.exec(s)) {
    console.log(result);
}

// [ 'a', index: 0, input: 'afg43223444wr423424243', groups: undefined ]
// [ 'f', index: 1, input: 'afg43223444wr423424243', groups: undefined ]
// [ 'w', index: 11, input: 'afg43223444wr423424243', groups: undefined ]
打赏作者
您的打赏是我前进的动力
微信
支付宝
评论

中午好👏🏻,我是 ✍🏻   疯狂 codding 中...

粽子

这有关于产品、设计、开发的问题和看法,还有技术文档和你分享。

相信你可以在这里找到对你有用的知识和教程

了解更多

目录

  1. 1. 捕获组
    1. 1.1. 具名捕获组
    2. 1.2. 非捕获组
  2. 2. 反向引用
  3. 3. 正向断言(预查)
  4. 4. 负向断言(预查)