Regex Lookaheads Explained With a Password Rule
One pattern for 12+ characters, a lowercase letter and a digit, tested on four inputs.
By the TurantTools Dev team · Updated October 27, 2026 · 2 min read
Lookaheads let one regex check several conditions at once without consuming characters. (?=...) means "what follows must match this", and the regex engine then goes back to where it started.
At least 12 characters, one lowercase letter and one digit
^(?=.*[a-z])(?=.*\d).{12,}$| Input | Result |
|---|---|
| correcthorse | No match |
| correcthorse9 | Match |
| short9 | No match |
| ALLCAPS123456 | No match |
- Each
(?=.*X)scans ahead for X anywhere in the string. - Negative lookahead
(?!...)forbids something, such as spaces:(?!.*\s). - For user-facing rules, separate checks give clearer error messages than one clever regex.