先行断言
- /x(?=y)x/ 匹配后边紧跟着'y'的'x'(分析要匹配内容的后边), 'y'不算是匹配内容的部分
const str = '23418423'
str.replace(/\d{1,3}(?=(\d{3})+$)/g, `$&,`)
const str = 'djI38D55'
const reg = return /^(?=.*[a-z])(?=.*\d)(?=.*[A-Z])[A-Za-z\d$@!%#?&.]{6,}$/g
reg.test(str)
后行断言
- /(?<=y)x/ 与先行断言相反, 匹配前边紧挨着'y'的'x'(分析要匹配内容的前边), 'y'不算是匹配内容的部分
const str = '23418423'
str.replace(/(?<=^(\d{3})+)\d{1,3}/g, `,$&`)
正向否定
- /x(?!y)x/ 匹配后边不紧跟着'y'的'x'(分析要匹配内容的后边), 'y'不算是匹配内容的部分
const str = '3.14'
const reg = /\d+(?!\.)/g
str.match(reg)
反向否定
- /(?<!y)x/ 与正向否定相反, 匹配前边不紧挨着'y'的'x'(分析要匹配内容的前边), 'y'不算是匹配内容的部分
const str = '-1024'
const reg = /(?<!-)\d+/g
str.match(reg)
参考
RegExp
replace