Positive and negative lookahead
Require suffix without consuming it
/foo(?=bar)/
Example:
/\d+(?=px)/
Exclude suffix patterns
/foo(?!bar)/
Example:
/\b\w+(?!\.tmp)\b/
Positive and negative lookbehind
Require prefix before match
/(?<=\$)\d+(?:\.\d{2})?/
Example:
/(?<=ID-)\d+/
Exclude prefix patterns
/(?
Example:
/(?
Combined assertions
Password rule example
/^(?=.*[A-Z])(?=.*\d).{8,}$/
Example:
/^(?=.*[a-z])(?=.*[!@#$%^&*]).{10,}$/
Token extraction with boundaries
/(?<=token=)[A-Za-z0-9_-]+/
Example:
/(?
Common mistakes / Pitfalls
- Lookbehind is unsupported in some older runtimes, especially legacy browsers.
- Nested lookarounds can hurt readability and performance in large texts.
- Zero-width assertions do not consume text, so global matching may seem surprising.
Last updated: February 2026