Does Your Language Support Regex Lookbehind?
Short answer: JavaScript (since ES2018), Python, Java, .NET and PCRE support lookbehind; Go's RE2 and most POSIX tools do not. Details, gotchas and working snippets below.
Support at a glance
| Engine | Lookbehind? | Variable-length? | Notes |
|---|---|---|---|
| JavaScript | Yes (ES2018) | Yes | (?<=\$)\d+ works in modern V8/SpiderMonkey |
Python re | Yes | No — fixed width only | Use the PyPI regex module for variable-length |
| Java | Yes | Bounded | Allows bounded quantifiers like (?<=a{1,3}) |
| PHP / PCRE | Yes | No (PCRE1); \K workaround | \K resets the match start as a lookbehind alternative |
| Go (RE2) | No | — | RE2 rejects lookaround by design (linear-time guarantee) |
Use it in your code
# Python: grab the number after a dollar sign, without capturing the $
import re
re.findall(r'(?<=\$)\d+', 'Price $42 and $7') # -> ['42', '7']
// JavaScript (ES2018+)
'Price $42 and $7'.match(/(?<=\$)\d+/g); // -> ['42', '7']
The variable-length trap
Python's built-in re requires a fixed-width lookbehind: (?<=abc) is fine, (?<=a+) raises error: look-behind requires fixed-width pattern. If you need variable-length lookbehind in Python, install the third-party regex module, which supports it. JavaScript and .NET allow variable-length lookbehind natively.
No lookbehind? Two escapes
- Capture and drop: match what precedes with a group and reference group 1 instead of the whole match — e.g.
\$(\d+)and read group 1. - PCRE
\K:\$\K\d+matches the$then resets the reported match start, so only the digits are returned.
FAQ
Does JavaScript support regex lookbehind?
Yes, since ES2018 (Node 10+, all modern browsers). Both positive (?<=...) and negative (?<!...) lookbehind work, including variable-length patterns.
Why does Python say 'look-behind requires fixed-width pattern'?
The built-in re module only allows fixed-width lookbehind. Use a fixed pattern, or install the PyPI 'regex' module which supports variable-length lookbehind.
How do I do lookbehind in Go?
You cannot — Go's RE2 engine has no lookaround. Capture the preceding text in a group and read that group, or restructure the match.