TSToolSphere
Back to all articles
regex

Regex Lookbehinds: Validating Preceding string parameters

2026-07-216 min read

Try it: free Regex Tester & Matcher

Test and match regular expressions in real-time with group extraction and replace helpers.

Open →

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+
Input:   "Price: $300, Quantity: 5"
Matches: "300" is excluded (preceded by $); "5" matches (not preceded by $)

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 historically lagged behind lookahead support across regex engines — some older JavaScript environments, for instance, didn't support lookbehinds at all until relatively recently, while lookaheads have been broadly supported for much longer. Always verify lookbehind support in your specific target environment (particularly older browsers or embedded regex engines) before relying on it.

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. This is a real, easy-to-hit portability trap: a lookbehind pattern that works in one language/engine can fail to even compile in another purely because of this length 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?
Depends on the engine — some only support fixed-length lookbehinds and will reject a variable-length one (like (?<=a+)) outright.

Test lookbehind patterns live against real sample input with the Regex Tester — entirely client-side.

Looking for other tools?

Explore ToolSphere Homepage →