Script Valley
Regex: Actually Useful Patterns
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

Positive lookahead

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.

Up next

Negative lookahead in regex to exclude patterns

Sign in to track progress

Positive lookahead in regex how it works โ€” Lookaheads and Lookbehinds โ€” Regex: Actually Useful Patterns โ€” Script Valley โ€” Script Valley