命名捕获分组
let str = '<a href="http://www/atguigu.com">尚硅谷</a>';
// 1.
const reg = /<a href="(.*)">(.*)<\/a>/;
const result = reg.exec(str);
// console.log(result);
/*
[ "<a href=\"http://www/atguigu.com\">尚硅谷</a>", "http://www/atguigu.com", "尚硅谷" ]
0: "<a href=\"http://www/atguigu.com\">尚硅谷</a>"
1: "http://www/atguigu.com"
2: "尚硅谷"]
*/
const reg1 = /<a href="(?<url>.*)">(?<text>.*)<\/a>/;
const result1 = reg1.exec(str);
console.log(result1);
/*
[ "<a href=\"http://www/atguigu.com\">尚硅谷</a>", "http://www/atguigu.com", "尚硅谷" ]
0: "<a href=\"http://www/atguigu.com\">尚硅谷</a>"
1: "http://www/atguigu.com"
2: "尚硅谷"]
//–––––––––––––– groups有值
groups: Object { url: "http://www/atguigu.com", text: "尚硅谷" }
*/
正向断言 反向断言
let str = "jseee你知道呢555啦啦啦";
// 正向断言
const reg = /\d+(?=啦)/;
const result = reg.exec(str);
console.log(result);
/*
Array [ "555" ]
0: "555"
groups: undefined
index: 9
input: "jseee你知道呢555啦啦啦"
length: 1
*/
// 反向断言
const reg1 = /(?<=呢)\d+/;
const result1 = reg.exec(str);
console.log(result1);
/*
Array [ "555" ]
0: "555"
groups: undefined
index: 9
input: "jseee你知道呢555啦啦啦"
length: 1
*/
网友评论