Regex Lookahead and Lookbehind Explained Simply
Published April 2025 · 5 min read
Lookaheads and lookbehinds match a position, not characters. They "look" without consuming.
Positive Lookahead (?=)
"Match X only if followed by Y"
\d+(?= dollars)
"100 dollars" → matches "100"
"100 euros" → no matchNegative Lookahead (?!)
"Match X only if NOT followed by Y"
\d+(?! dollars)
"100 euros" → matches "100"
"100 dollars" → no matchPositive Lookbehind (?<=)
"Match X only if preceded by Y"
(?<=\$)\d+
"$100" → matches "100"
"€100" → no matchNegative Lookbehind (?<!)
"Match X only if NOT preceded by Y"
(?<!\$)\d+
"€100" → matches "100"Practical Examples
- Password must have a number:
(?=.*\d) - Extract price without $:
(?<=\$)[\d.]+ - Match word not after "not ":
(?<!not )good
Test It
Use our Regex Tester to experiment with lookaheads.