Lookaround
NoteThis section is a Work In Progress.
Lookarounds can be used to verify conditions, without matching any text.
You’re only looking, not moving.
- Lookahead
- Positive —
(?=…)
- Negative —
(?!…)
- Lookbehind
- Positive —
(?<=…)
- Negative —
(?<!…)
Lookahead
Positive
Note how the character following the _
isn’t matched. Yet, its nature is confirmed by the positive lookahead.
After (?=[aeiou])
, the regex engine hasn’t moved and checks for (?=\1)
starting after the _
.
Negative
Without the anchors, this will match the part without the #
in each test case.
Negative lookaheads are commonly used to prevent particular phrases from matching.
Lookbehind
Limited SupportJavaScript, prior to ES2018, did not support this flag.
Positive
Negative
Examples
Password validation
Lookarounds can be used verify multiple conditions.
Quoted strings
Without lookaheads, this is the best we can do: