ES6断言(瞻仰)

479 阅读1分钟
let str1 = '20%25%34%40%50px60px70px30px';
// 先行断言,/x(?=y)/ 匹配前边是y的x数据
let s = str1.match(/\d+(?=%)/g) // ["20", "25", "34", "40"]
// 先行否定断言,/x(?!y)/ 匹配前边不是y的x数据
let s1 = str1.match(/\d{2}(?!%)/g) // ["50", "60", "70", "30"]
// 后行断言,/(?<=x)y/ 匹配后边是x的y数据
let s2 = str1.match(/(?<=%)\d{2}/g) // ["25", "34", "40", "50"]
// 后行否定断言 /(?<!x)y/ 匹配后边不是x的y数据
let s3 = str1.match(/(?<!%)\d{2}/g) // ["20", "60", "70", "30"]
console.log(s3)