Lookaheads and LookbehindsLesson 3.1
Positive lookahead in regex how it works
positive lookahead syntax, zero-width assertion, match vs consume, practical use cases, password validation example
Lookaheads Check Without Consuming
A lookahead asserts that what follows the current position matches a pattern — but it does not consume characters. The match position stays where it was. Syntax: (?=pattern).
// Match 'foo' only when followed by 'bar'
/foo(?=bar)/.exec('foobar') // matches 'foo' at index 0
/foo(?=bar)/.exec('foobaz') // null — 'baz' != 'bar'
Multiple Lookaheads
// At least 8 chars, one digit, one uppercase
const strong = /^(?=.*\d)(?=.*[A-Z]).{8,}$/;
strong.test('Password1') // true
Lookaheads do not change what gets captured — they only gate whether the main pattern matches.
