开发者

Can `(?=regex)` and `(?!regex)` be used in the middle of a regular expression?

开发者 https://www.devze.com 2022-12-23 01:38 出处:网络
In JavaScript, can (?=regex) and (?!regex) be used in the middle of a regular开发者_如何学Go expression, or they must be used at the end of a regular expression? If they can be used in the middle, wha

In JavaScript, can (?=regex) and (?!regex) be used in the middle of a regular开发者_如何学Go expression, or they must be used at the end of a regular expression? If they can be used in the middle, what's the meaning of it?


(?=…) and (?!…) (lookaheads) asserts that the string after it matches/does not match the sub-regex, without actually consuming them. They can appear anywhere. For example

^(\d)(?!\1)\d+$

matches

12345
67890

but not

11234
55678

See http://www.regular-expressions.info/lookaround.html for detail.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号