← Back to Blog

Regex Lookahead & Lookbehind Explained With Examples

Master regex lookahead and lookbehind assertions: positive/negative, practical examples for password validation, parsing, and data extraction.

Lookahead: Match Without Consuming

// Positive lookahead: X followed by Y\n\d+(?= dollars)    // matches '100' in '100 dollars'\n\n// Negative lookahead: X NOT followed by Y\n\d+(?! dollars)    // matches '100' in '100 euros'

Lookbehind

// Positive lookbehind: preceded by X\n(?<=\$)\d+         // matches '100' in '$100'\n\n// Negative lookbehind: NOT preceded by X\n(?<!\$)\d+         // matches '100' in '€100'

Password Validation Example

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

Requires: lowercase, uppercase, digit, symbol, 8+ chars.

Try It Free

Use our free online tool — 100% client-side, no data leaves your browser.

Open Regex Tester

Related Tools & Articles