Script Valley
Regex: Actually Useful Patterns
Lookaheads and LookbehindsLesson 3.3

Lookbehind assertions in regex positive and negative

positive lookbehind syntax, negative lookbehind syntax, browser support, extracting currency amounts, lookbehind limitations variable length

Lookbehinds Check What Came Before

Lookbehind assertions

Positive lookbehind (?<=pattern) succeeds when the preceding text matches. Negative lookbehind (?<!pattern) succeeds when it does not. Both are zero-width.

// Extract dollar amounts without the $ sign
'$10 €20 $30'.match(/(?<=\$)\d+/g)
// => ['10', '30']

// Negative: digits NOT preceded by $
'$10 €20 £30'.match(/(?<!\$)\d+/g)
// => ['20', '30']

Lookbehinds were added to JavaScript in ES2018 (V8 6.3+). All modern browsers support them.

Up next

Combining lookaheads and lookbehinds for complex conditions

Sign in to track progress