令人头痛的正则,前瞻后顾,负前瞻,负后顾

2019-08-13  本文已影响0人  louiebb
A(?=B) 前瞻(Look ahead positive):

匹配的是A,限制条件是A的后面是B,即AB。

(?<=B)A 后顾(Look behind positive ):

匹配表达式A,限制条件A的前面是B,即BA

A(?!B) 负前瞻(Look ahead negative):

顾名思义,该正则匹配A,限制条件是A后面不是B

(?<!B)A 负后顾(Look behind negative ): (有兼容性不支持)

匹配表达式A,限制条件是A的前面不是B

'>>3 <div>>我是内容<div/>>3'.replace(/(?<!(\<(([a-zA-Z])(\/)?)+))\>/g, '&gt;')
匹配>:>负后顾,匹配>前面不是<英文或<英文/
'<3 3<div>我是内容3<div/>'.replace(/\<(?!(([a-zA-Z])+(\/)?)\>)/g, '&lt;') 
匹配< : 负前瞻,匹配<后面不是英文>或英文/>
上一篇 下一篇

猜你喜欢

热点阅读