Most regex patterns describe the text you want to capture. Lookaheads and lookbehinds solve the other half of the problem: the text you want to require, or forbid, around the match without capturing it. You want the numbers that are followed by a unit, but not the unit itself. A plain pattern cannot express that: anything it matches becomes part of the result.

This guide explains the four lookaround forms, shows each one working in the free Regex Tester on Ovatly, and covers the mistakes that make lookarounds look broken when they are doing exactly what they were told.

Contents

  • Quick answer
  • What a lookaround actually does
  • The four forms
  • How to test a lookaround, step by step
  • Worked example: a positive lookahead for pixel values
  • Worked example: a negative lookahead for q without u
  • Worked example: lookbehinds and dollar amounts
  • The JavaScript flavor point
  • Limitations
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

A lookahead checks what follows a position in the text; a lookbehind checks what precedes it. Neither consumes characters, so the checked text is not part of the match. Positive lookahead (?=...) requires the contents to follow, negative lookahead (?!...) forbids them, positive lookbehind (?<=...) requires the contents to precede, and negative lookbehind (?<!...) forbids them. To test one, open the Regex Tester, type the pattern without slashes, put flags such as g in the Flags field, paste your text, and read the match list.

What a lookaround actually does

A lookaround is a zero-width assertion. At a given position, it asks a yes or no question about the neighbouring characters: does this text follow me, or precede me? If yes, matching continues from that same position; if no, the position fails and the engine moves on. The lookaround matches no characters, which is why the text it checks never shows up in the result.

That property explains both the power and the confusion. The power: you can select numbers because of the unit after them, while leaving the unit behind. The confusion: people expect the checked text in the match, and think the pattern is broken when it is absent.

The four forms

The syntax has two choices: direction (ahead or behind) and sign (positive: the contents must be there; negative: they must not).

  • Positive lookahead: (?=...) requires the contents to follow.
  • Negative lookahead: (?!...) forbids the contents from following.
  • Positive lookbehind: (?<=...) requires the contents to precede.
  • Negative lookbehind: (?<!...) forbids the contents from preceding.

Placement matters: a lookahead sits right after the part of the pattern it guards, a lookbehind just before it, as if the engine were standing there looking back.

How to test a lookaround, step by step

  1. Open the Regex Tester. Type the core pattern first, without any lookaround and without slashes, in the Pattern field. For the first example below, that is \d+.
  2. Add the flags you need in the Flags field. Use g to list every match; without it, only the first match is reported.
  3. Paste or type the test text.
  4. Read the match list: each match shows its text, index and capture groups. Confirm the plain pattern finds what you expect before adding any condition.
  5. Add one lookaround and compare the match list. Invalid patterns get a plain-language error from the tester, not a stack trace, which makes this loop fast.

Worked example: a positive lookahead for pixel values

Goal: pull the numbers that are pixel values out of a line of CSS-like text, without the units.

Pattern: \d+(?=px) with flag g. Test text:

Set width: 24px, height: 10em, gap: 8px, indent: 2rem

The tester reports two matches: 24 at index 11 and 8 at index 36. The numbers before em and rem are skipped, because after them the lookahead does not find px. And notice what the matches contain: digits only. The px was checked, not consumed, so a find-and-replace can rewrite the numbers and leave every unit untouched.

Worked example: a negative lookahead for q without u

Goal: find the letter q where it is not followed by u, as happens in transliterated words.

Pattern: q(?!u) with flag g. Test text:

qatar, qat, quit, qwerty, iraq

The tester reports four matches, in qatar, qat, qwerty and iraq. The q in quit is skipped: the next character is u, so the negative lookahead fails. The q at the end of iraq matches because nothing follows it, and "not followed by u" is true when nothing follows at all. Remember that edge case: a negative lookaround passes at the end of the text, and a positive one fails there.

Worked example: lookbehinds and dollar amounts

Goal: extract prices, meaning numbers that come right after a dollar sign.

Pattern: (?<=\$)\d+ with flag g. Test text:

The ticket costs $45, the meal $12.50, and the taxi 20 dollars.

The tester reports 45 at index 18 and 12 at index 32. The 20 is skipped because a space, not a dollar sign, precedes it. The 50 in the meal price is also skipped: it is preceded by a full stop, and a lookbehind inspects only the characters immediately before the match.

The negative form answers the opposite question. Pattern (?<!\$)\b\d+\b with flag g, against the text Order 123 ships in 45 days and costs $99 today, reports 123 at index 6 and 45 at index 19, while the 99 is excluded because the position before it holds a dollar sign. The word boundaries matter here: without them, the engine could start matching at the second 9, where the preceding character is a 9 rather than the sign.

The JavaScript flavor point

The tester uses JavaScript's RegExp engine, so its syntax follows JavaScript rules, which differ slightly from PCRE, Python or grep flavors, as the tool's notes state. Lookbehind is the clearest example: it works in the tester and in modern browsers, but it is not available in every engine. Treat a pattern that passes in the tester as proven for JavaScript, and as a strong draft, not a guarantee, for any other flavor. Test again in the target engine before shipping it.

Limitations

A lookaround sees only the immediate neighbourhood of a position. It cannot express "somewhere later in the document", compare two captured values, or count. The exclusion cuts both ways: because the checked text is not consumed, a later part of the pattern can still match the same characters, which surprises people building replacements. The tester caps results at 500 matches, and heavily nested patterns can make a browser tab busy through catastrophic backtracking; simplify the pattern if that happens, as the tool's notes advise.

Common mistakes

  • Typing the pattern with slashes. The Pattern field takes the pattern body only, like \d{3}-\d{4}; flags belong in their own field.
  • Expecting the lookahead text inside the match. It is checked, never included. If you need it in the result, move it outside the lookaround.
  • Forgetting the g flag. Without it, the tester reports the first match only, which looks like the lookaround rejected everything else.
  • Stacking all four forms at once. When a combined pattern fails, nothing tells you which condition failed. Add one lookaround at a time.
  • Assuming portability. A lookbehind that runs in the tester may not run in your server's regex flavor at all.

FAQs

What is the difference between a lookahead and a lookbehind? A lookahead checks the text after the current position, and a lookbehind checks the text before it. Neither consumes characters: the checked text is never part of the match, which is why a lookahead is the right tool when you want a number but not the unit after it.

Why does my lookahead match less text than I expected? Because the lookahead only tests a condition, it adds nothing to the match. With the pattern for digits followed by px, the match is the digits alone and the px stays outside. If you need the unit inside the match, write it as a normal part of the pattern instead.

Do lookbehinds work in every regex flavor? No. The tester runs JavaScript's RegExp engine, and modern browsers support lookbehind, but flavors differ: the tool's own notes warn that JavaScript syntax differs slightly from PCRE, Python and grep. A pattern that works in the tester may need adjusting in another engine, or may not be portable at all.

Why did the tester report only the first match? Without the g flag, only the first match is reported, matching how most code uses a pattern. Add g in the Flags field when you want every match listed with its index and groups.

Can a lookaround check text far away from the match? No. A lookaround only inspects the text immediately before or after the current position. It cannot relate two parts of a document, count, or compare values; it answers a yes or no question about the neighbouring characters.

Conclusion

Lookarounds turn a pattern from a description of text into a description of context: match this, but only when that surrounds it. Build them one condition at a time in the Regex Tester, read the match list after each change, and keep the JavaScript flavor in mind. The same match-list workflow powers a different job, finding doubled words in a draft, in our guide to finding duplicate words in text with regex. For more like this, see the Developer Tools archive.