Positive Lookbehind
Lookbehinds check what comes BEFORE a position. Positive lookbehind matches when preceded by a specific pattern.
Syntax
(?<=lookbehind)pattern matches "pattern" only if preceded by "lookbehind".
Matches numbers only when preceded by "$" - but "$" isn't part of the match!
Exercises
Lookbehind vs Regular Match
Compare these approaches:
With lookbehind - Only the value is matched:
Without lookbehind - Everything is matched:
Lookbehind is cleaner when you only want the value.
Practical Examples
Combining with Lookahead
Use both for precise matching:
Practice Playground
Try:
(?<=@)\w+- after @(?<=\$)[\d.]+- after $(?<=#)\w+- after #(?<=: ).+- after ": "
Lookbehind Limitations
Lookbehinds must have a fixed length in most regex engines:
- ✅
(?<=abc)- fixed 3 characters - ✅
(?<=ab|cd)- fixed alternatives - ❌
(?<=a+)- variable length (not allowed in many engines)
JavaScript supports variable-length lookbehinds, but not all languages do.
Key Takeaways
(?<=pattern)is a positive lookbehind- Matches only when preceded by the pattern
- Zero-width: doesn't consume characters
- Cleaner than capturing groups for extraction
- Combine with lookahead for precise matching

