Flowfiles ← Regex Tester

Regex Lookahead & Lookbehind

(?=…) · (?!…) · (?<=…) · (?<!…) — Zero-width assertions — online tester

Lookaheads and lookbehinds let you match positions in text based on what comes before or after, without including that context in the match. Test them live with the Explain feature to see each assertion decoded.

Paste a lookahead pattern and see which positions match — the Explain feature breaks down each assertion.

Open the Regex Tester →

The four zero-width assertions

(?=…)

Positive lookahead — position must be followed by the pattern.
Example: \d+(?= USD) matches numbers followed by " USD".

(?!…)

Negative lookahead — position must NOT be followed by the pattern.
Example: \bfoo(?!bar)\b matches "foo" not followed by "bar".

(?<=…)

Positive lookbehind — position must be preceded by the pattern.
Example: (?<=\$)\d+ matches digits preceded by "$".

(?<!…)

Negative lookbehind — position must NOT be preceded by the pattern.
Example: (?<!\d)\d{4}\b matches 4-digit numbers not part of a longer number.

Since they are zero-width, the text inside the assertion is never included in the match — only the position is tested.

// Extract price values without the $ sign
const prices = '$12.50 and $3.99'.match(/(?<=\$)\d+\.\d{2}/g);
// → ["12.50", "3.99"]

// Match words not followed by a comma
const words = 'cat, dog bird'.match(/\b\w+\b(?!,)/g);
// → ["dog", "bird"]

Frequently asked questions

What is a zero-width assertion?

A zero-width assertion matches a position in the string rather than a span of characters. Lookaheads and lookbehinds are zero-width: they check the context but do not consume any characters. This means the matched text never includes the lookahead/lookbehind pattern.

Can I combine lookahead and lookbehind?

Yes. (?<=\$)\d+(?=\.) matches digits preceded by "$" and followed by ".". Combining multiple assertions at the same position is valid and all must hold simultaneously.

Are variable-length lookbehinds supported?

Yes in V8 (Chrome/Node.js) and SpiderMonkey (Firefox 78+). The ECMAScript spec now allows variable-length lookbehinds like (?<=\w+). Some older regex engines (PCRE) require fixed-length lookbehinds.

How do I debug a complex lookahead?

Paste the pattern in the Regex Tester and click Explain regex. Each assertion is decoded separately. You can also simplify the pattern by testing just the lookahead portion in isolation to verify it matches the right positions.

Related tools