views:

33

answers:

1

Hi guys,

In JavaScript, can (?=regex) and (?!regex) be used in the middle of a regular 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? Great thanks!

+3  A: 

(?=…) 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.

KennyTM