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
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.
