Regex Lookahead and Lookbehind Explained (With Examples)
How regex lookahead (?=) and lookbehind work, with tested examples for prices, IDs, file names and passwords, plus which languages support lookbehind.
The short answer
Lookahead and lookbehind are zero-width assertions: they check what comes after or before the current position without including it in the match. \d+(?=px) matches the number in 10px but not the px; (?<=INV-)\d+ matches 2041 in INV-2041 but not the prefix. You can try every pattern below in the Regex Tester, which runs your pattern with your browser's JavaScript regex engine.
The four lookaround types
| Syntax | Name | Meaning |
|---|---|---|
X(?=Y) | Positive lookahead | Match X only if Y comes right after it |
X(?!Y) | Negative lookahead | Match X only if Y does not come right after it |
(?<=Y)X | Positive lookbehind | Match X only if Y comes right before it |
(?<!Y)X | Negative lookbehind | Match X only if Y does not come right before it |
The part inside the brackets is tested but never "consumed". That has two practical effects:
- The text inside the lookaround is not part of the match, so it is not highlighted, not returned and not replaced.
- The regex engine stays at the same position after the check, so several lookarounds can test the same spot one after another.
Positive lookahead: match something only if it is followed by something
Say you want the numbers that are pixel values in 10px 20em 30px.
- Pattern:
\d+(?=px) - Flags:
g - Matches:
10and30
Without the lookahead, \d+px would match 10px and 30px. That works if you want the unit too, but if you are pulling out the numbers (or replacing only the numbers), the lookahead keeps px out of the result.
Another common use is picking an amount by its currency. In Total: 450 USD, 300 INR, the pattern \d+(?= INR) matches only 300. Note the space inside the lookahead: it must match the text exactly as it appears.
Negative lookahead: exclude what follows
foo(?!bar) matches foo only when it is not followed by bar. In foobar foobaz it matches once, the foo at position 7 (the one in foobaz).
Negative lookahead at the start of a pattern is a handy way to say "the whole line must not contain this". To match file names that do not end in .pdf:
- Pattern:
^(?!.*\.pdf$).+$ - Tested against
report.pdf,photo.jpgandnotes.txtone at a time, it matchesphoto.jpgandnotes.txtonly.
The .* inside the lookahead lets it look ahead across the whole line, which is why the check works even though .pdf is at the end.
Stacking lookaheads: the password-rule pattern
Because lookaheads do not move the position, you can stack them at the start of the string and each one checks the whole input independently:
- Pattern:
^(?=.*\d)(?=.*[A-Z]).{8,}$ Secret123matches (has a digit, an uppercase letter and 8+ characters).secret123does not match (no uppercase letter).
Read it as: "from the start, somewhere ahead there is a digit; also somewhere ahead there is an uppercase letter; then match 8 or more characters to the end". The full set of password rules is covered in regex for password validation.
Positive lookbehind: match something only if it is preceded by something
Lookbehind does the same job looking backwards.
- Text:
INV-2041 PO-77 INV-9 - Pattern:
(?<=INV-)\d+ - Flags:
g - Matches:
2041and9
The PO-77 number is skipped because it is not preceded by INV-. For prices, (?<=\$)\d+ against price: $100, €80 and $250 returns 100 and 250. The dollar sign is escaped as \$ because a bare $ means "end of string" in regex.
Negative lookbehind: exclude what comes before
(?<![$\d])\d+ against the same price text returns only 80. The negative lookbehind says "the character before this digit run must not be a dollar sign or another digit". Including \d in the set matters: without it, (?<!\$)\d+ would still match 00 inside $100, because the engine can start the match at the second digit, where the character before is 1, not $.
This "start in the middle" behaviour is the most common surprise with negative lookbehind. If you only want whole numbers, block digits as well as the prefix, or add a word boundary.
A useful combined example: thousands separators
Lookarounds can match an empty position, which is perfect for inserting characters. In JavaScript:
"1234567".replace(/\B(?=(\d{3})+(?!\d))/g, ",")returns1,234,567.
\B finds positions inside the number, the lookahead requires a multiple of three digits after that position, and the inner (?!\d) makes sure those groups run to the end of the number. Nothing is consumed, so the replacement inserts a comma rather than replacing digits. (This gives Western grouping; Indian lakh-crore grouping needs a different pattern.)
Which languages support lookbehind?
| Engine | Lookahead | Lookbehind |
|---|---|---|
| JavaScript (modern browsers, Node.js) | Yes | Yes, including variable length |
Python re | Yes | Fixed width only |
Other engines (PHP's PCRE, Java, .NET, Go) differ again, and Go's standard regexp package supports no lookarounds at all, so check your language's documentation before relying on lookbehind. We checked the Python row in Python 3: re.findall(r'(?<=INV-)\d+', 'INV-2041 PO-77 INV-9') returns ['2041', '9'], but compiling (?<=\d+)x raises "look-behind requires fixed-width pattern". The same pattern compiles in JavaScript. If a lookbehind works in the tester but fails in your code, this is a likely reason; see why a regex works in the tester but not in your code.
How to test lookarounds in the Regex Tester
(?<=INV-)\d+.g in the flags box to see every match, not just the first. Add i for case-insensitive and m if you use ^ and $ on each line of multi-line text.Note that the highlighted text never includes the lookaround part. If you see INV- highlighted, you have written a normal group, such as (INV-), rather than a lookbehind.
Common mistakes
- Writing
(?=when you meant(?:.(?:...)is a non-capturing group that consumes text;(?=...)only checks. Mixing them up changes what is matched. - Forgetting that lookarounds are anchored to the current spot.
(?=px)\d+never matches, because it asks forpxand a digit at the same position. - Expecting a lookahead to search the whole string.
(?=\d)checks only the very next character. Use(?=.*\d)to mean "a digit somewhere ahead". - Overusing lookarounds. If you need the surrounding text anyway, a capture group is often simpler:
INV-(\d+)returns the number in group 1. The Find and Replace tool can then use$1in the replacement. - Using variable-length lookbehind in Python. Rewrite it with a fixed-width lookbehind or a capture group.
Lookaround vs capture group: which to use
Use a capture group when you are extracting data and your language makes groups easy to read. Use a lookaround when the match itself must exclude the surrounding text, for example in a find-and-replace where you want to change the number but keep the prefix, or when you need several independent conditions on the same text, as in password rules. Both approaches are explained with more examples in the regex beginner's guide.
Frequently asked questions
What is the difference between lookahead and lookbehind in regex?
Lookahead checks the text after the current position, lookbehind checks the text before it. Neither includes the checked text in the match. For example, \d+(?=px) matches 10 in 10px, and (?<=INV-)\d+ matches 2041 in INV-2041.
Does JavaScript support regex lookbehind?
Yes. Modern browsers and Node.js support both positive (?<=...) and negative (?<!...) lookbehind, including variable-length patterns. Python's re module supports lookbehind only with a fixed width.
Why does my negative lookbehind still match part of a number?
The engine can start a match in the middle of a number. (?<!\$)\d+ matches 00 inside $100 because the character before the second digit is 1, not $. Add \d to the lookbehind, as in (?<![$\d])\d+, to block that.
Is a lookahead the same as a non-capturing group?
No. A non-capturing group (?:...) consumes the text it matches. A lookahead (?=...) only checks that the text is there and leaves the position unchanged.
Put this guide into practice with our free online tool — no signup required.
Open tool