Concept
Lookahead and Lookbehind
Lookarounds assert a condition at a position without consuming characters. (?=X) is lookahead, (?<=X) is lookbehind. ! negates them.
Four flavors
Lookahead (?=X) matches if X follows. Negative lookahead (?!X) matches if X does NOT follow. Lookbehind (?<=X) matches if X precedes. Negative lookbehind (?<!X) matches if X does NOT precede. All four are zero-width — they don't consume characters, they just gate the match.
Positive lookahead example
Use lookahead to match something only when followed by a specific suffix, without including that suffix in the result.
Capture the number before %
Try this/\d+(?=%)/Input
Growth: 42% YoYResult
Match: 42 (the % is not consumed)Negative lookahead example
Negative lookahead is perfect for 'anything EXCEPT when this specific thing follows.' Password validation commonly chains multiple lookaheads to assert character-class requirements without anchoring to a specific order.
Strong password: has lowercase, uppercase, digit, special, min 8
Try this/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{8,}$/Input
MyP@ssw0rdResult
MatchLookbehind caveats
Lookbehind support in JavaScript is solid (ES2018+). Variable-length lookbehind is also supported but older engines require fixed-length. Position matters: (?<=\$)\d+ matches digits that come right after a dollar sign, without including the $ in the result.