What is difference between (?=regex)
and (?:regex)
in JavaScript?
Explain it with an example,Please.
What is difference between (?=regex)
and (?:regex)
in JavaScript?
Explain it with an example,Please.
(?=regex)
is a positive lookahead, it matches a group after the main expression without including it in the result.
Example:
\d(?=px)
1pt 2px 3em 4px
This will only match the 2
and the 4
, not the entire 2px
and 4px
.
(?:regex)
is a non-capturing group, it groups multiple tokens together without creating a capture group.
Example:
(?:ha)+
hahaha haa hah!
This will match each ha
, but not create a group for it.