TExcelTAD
Home › Regular Expressions › Lookbehind

Lookbehind

A lookbehind is the mirror image of a lookahead: it checks the text just before the current position, again without including it in the match.

Syntax Name Succeeds when the text behind…
(?<=Y)X positive lookbehind …matches Y
(?<!Y)X negative lookbehind …does not match Y

JavaScript has supported lookbehind since ES2018, and all current browsers support it.

Positive lookbehind

Get prices without the currency symbol:

regex · try it yourself
/(?<=\$)\d+(?:\.\d\d)?/g
Lunch $12.50, coffee $3, tip 2
2 matches
#1 "12.50" at index 7
#2 "3" at index 22

tip 2 isn't preceded by $, so it's ignored. The $ itself isn't part of either match.

Negative lookbehind

Numbers that are not preceded by a minus sign or a digit (the digit check stops the engine from matching the tail of -15):

regex · try it yourself
/(?<![-\d])\d+/g
5 -15 20 -3
2 matches
#1 "5" at index 0
#2 "20" at index 6

Lookbehind + lookahead: text "between"

Combine both to grab what's between two markers while leaving the markers out:

regex · try it yourself
/(?<=\[)[^\]]+(?=\])/g
Tags: [news] [tech] [] [ai]
3 matches
#1 "news" at index 7
#2 "tech" at index 14
#3 "ai" at index 24

Often a capturing group does the same job (\[([^\]]+)\]). Lookarounds shine when you replace — only the inner text is matched, so only it is replaced:

js · try it yourself
const s = "Tags: [news] [tech]";
console.log(s.replace(/(?<=\[)[^\]]+(?=\])/g, (t) => t.toUpperCase()));
Tags: [NEWS] [TECH]

Lookbehind length: flavour differences

The text inside a lookbehind must be matched backwards from the current position. Engines differ in what they allow:

Engine Lookbehind support
JavaScript (ES2018+) any pattern, including * and +
.NET any pattern
Python re fixed width only: (?<=ab) ok, (?<=a+) error
Java bounded width: (?<=a{1,3}) ok, (?<=a+) error
PCRE / PHP fixed width per alternative, e.g. (?<=a|bcd) (PCRE2 10.43+ also allows bounded lengths)
Go regexp, Rust regex not supported

JavaScript's freedom lets you write things like "a word that comes after id: and any spaces":

regex · try it yourself
/(?<=id:\s*)\w+/g
id: abc  id:xyz   id:    q1
3 matches
#1 "abc" at index 4
#2 "xyz" at index 12
#3 "q1" at index 25

Python rejects the same idea, because \s* has no fixed width:

python · try it yourself
import re

try:
    re.findall(r"(?<=id:\s*)\w+", "id: abc")
except re.error as e:
    print("error:", e)

print(re.findall(r"id:\s*(\w+)", "id: abc  id:xyz   id:    q1"))
error: look-behind requires fixed-width pattern
['abc', 'xyz', 'q1']

The portable workaround is the one on the last line: match the prefix normally and capture the part you need.

Word boundaries with Unicode, via lookarounds

\b in JavaScript only understands ASCII \w. With the u flag you can build your own Unicode-aware boundary from lookarounds and \p{L} (any letter):

regex · try it yourself
/(?<!\p{L})phở(?!\p{L})/gu
phở, phởbò, ăn phở
2 matches
#1 "phở" at index 0
#2 "phở" at index 15

Test yourself

What does /(?<=#)\w+/g match in #red and #blue?

  1. #red, #blue
  2. red, blue
  3. #
  4. Nothing

Which pattern means "a digit not preceded by a dollar sign"?

  1. (?<=\$)\d
  2. (?<!\$)\d
  3. \d(?!\$)
  4. [^$]\d

Why does Python reject (?<=a+)b?

  1. Python has no lookbehind
  2. Python's lookbehind must have a fixed width
  3. + is not allowed in Python
  4. It needs the u flag