(?=…) · (?!…) · (?<=…) · (?<!…) — 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 →(?=…)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.
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.
Yes. (?<=\$)\d+(?=\.) matches digits preceded by "$" and followed by ".". Combining multiple assertions at the same position is valid and all must hold simultaneously.
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.
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.