The mirror image of lookaheads
Where a lookahead (?=...) checks what follows the current position, a lookbehind checks what precedes it — in both cases, without consuming or capturing the checked text.
Positive lookbehind: (?<=...)
Asserts that a specific pattern immediately precedes the current position:
Pattern: (?<=\$)\d+
Input: "Price: $300"
Match: "300" (the "$" must precede it, but isn't part of the match)
This is useful anywhere you need to match a value only when a specific marker precedes it, without including that marker in the extracted result — extracting a price after a currency symbol, or a value after a specific label, are typical cases.
Negative lookbehind: (?<!...)
Asserts the opposite — that a specific pattern does not immediately precede the current position:
Pattern: (?<![$\d])\d+
Input: "Price: $300, Quantity: 5"
Matches: "5" only
Note the \d inside the lookbehind. The obvious version, (?<!\$)\d+, returns "00" and "5": the engine fails at the "3", then simply starts one character later, where the preceding character is "3", not "$". Excluding digits as well as the "$" from the lookbehind (or starting with \b) stops it from matching the tail of a number.
Why lookbehinds arrived later, and vary more in support
Lookbehinds are computationally trickier to implement than lookaheads, since checking backward from a given position isn't as naturally aligned with how most regex engines scan input left to right. As a result, lookbehind support lagged behind lookahead support. JavaScript only gained lookbehinds in ES2018, and Safari only supported them from version 16.4 (2023), so code that must run on older iPhones should avoid them. POSIX tools such as grep -E and sed have no lookarounds at all (GNU grep -P does).
Variable-length lookbehinds: a further compatibility wrinkle
Some regex engines only support fixed-length lookbehinds ((?<=abc), always exactly 3 characters), rejecting variable-length ones ((?<=a+), which could match 1 or more characters) that other, more permissive engines do allow:
| Engine | (?<=a+)b |
|---|---|
| JavaScript (ES2018+), .NET | Allowed: any length |
| Java | Accepted by recent versions (tested on Java 21); older ones required a bounded length such as (?<=a{1,5}) |
Python re |
Error: "look-behind requires fixed-width pattern" |
| PCRE (PHP) | Each alternative must be fixed-length, e.g. (?<=ab|abc) |
This is an easy-to-hit portability trap: a pattern written and tested in a browser can fail to compile in Python. The third-party regex package for Python lifts the restriction.
Common mistakes
- Assuming lookbehind support is as universal as lookahead support. It isn't — check your specific target environment, especially anything running in older browsers or constrained regex implementations.
- Writing a variable-length lookbehind without checking engine support. Some engines only accept fixed-length lookbehinds — a pattern that compiles in one language may throw a compilation error in another.
- Confusing lookbehind direction.
(?<=...)checks backward (what precedes); it's easy to mentally flip this with lookahead's forward-checking direction, especially when reading a dense pattern quickly.
FAQ
What's the difference between a lookahead and a lookbehind?
A lookahead (?=...) checks what follows the current position; a lookbehind (?<=...) checks what precedes it — both without consuming the checked text.
Are lookbehinds supported everywhere lookaheads are?
No — lookbehind support has historically lagged and still varies more across regex engines and language versions; always verify support in your specific target environment.
Can a lookbehind check for variable-length preceding text?
In JavaScript, .NET and recent Java, yes (older Java needed a bounded length such as a{1,5}). Python's re and PCRE require fixed-length lookbehinds and reject (?<=a+) outright.
Test lookbehind patterns live against real sample input with the Regex Tester — entirely client-side.
Use the Text Diff & Compare to compare your sample text before and after an edit. For a related walkthrough, read Regular Expressions: Patterns and Matches.