Bulk Replace
Regex

Regex Lookahead and Lookbehind Explained With Find and Replace Examples

Learn regex lookahead and lookbehind with practical find and replace examples: match before or after text, exclude words and add thousand separators.

Updated 5 minute read The Bulk Replace team

Lookahead and lookbehind let a regex check what comes after or before a match without including that text in the match. \d+(?=px) matches the 12 in 12px but leaves px alone. (?<=\$)\d+ matches the 25 in $25 but not the dollar sign. The negative forms do the opposite: (?!...) means "not followed by" and (?<!...) means "not preceded by". In a find and replace, that means you can change text based on its surroundings without having to capture and rebuild those surroundings.

Below are the four forms, followed by practical find and replace examples you can adapt, and the differences between Windows tools.

The four look-arounds

SyntaxNameSucceeds whenExample
(?=x)Positive lookaheadx comes next\d+(?=px) matches 12 in 12px
(?!x)Negative lookaheadx does not come next\bcolor(?!s) matches color, not colors
(?<=x)Positive lookbehindx comes just before(?<=\$)\d+ matches 25 in $25
(?<!x)Negative lookbehindx does not come just before(?<!\$)\b\d+\b skips $20

All four are zero-width: they test a position, and the engine then carries on from the same spot. That is the key idea. Whatever is inside a look-around is checked but never consumed, so it is never replaced.

Why use a look-around instead of a capture group?

You can often get the same result with capture groups. To add https only to links on your own domain you could write either:

  • Capture: find http://(example\.com), replace https://$1
  • Look-around: find http://(?=example\.com), replace https://

Both work. Look-arounds shine in three situations:

  1. Conditions you do not want to repeat. The replacement stays short because you are not putting the context back.
  2. "Not" conditions. Capture groups cannot express "not followed by". Negative look-arounds can.
  3. Inserting text at a position. A pattern made only of look-arounds matches an empty position, so the replacement is inserted there without deleting anything.

If capture groups are new to you, read regex capture groups in find and replace first.

Practical find and replace examples

1. Upgrade links except for localhost

Find http://(?!localhost\b) and replace with https://. The negative lookahead skips development links such as http://localhost:8080, while every other http:// becomes https://. The full migration is covered in changing http to https in all files.

2. Insert a prefix only where it is missing

To add your brand to every page title, find (?<=<title>)(?!Acme \| ) and replace with Acme | . The lookbehind finds the position right after <title>, and the negative lookahead skips titles that already start with the brand. Running it twice does no harm, which makes it safe to repeat when new pages are added.

3. Change a number but not its unit

Find \b14(?=px) and replace with 16. Every 14px becomes 16px, but 14em and 140px are untouched. The \b stops it matching the end of 114px.

4. Add thousand separators to numbers

Find \B(?=(\d{3})+(?!\d)) and replace with a comma. It turns 1234567 into 1,234,567. The lookahead says "the rest of this number is a multiple of three digits", and \B stops a comma being put at the very start. Two cautions:

  • It also changes years, so 2025 becomes 2,025. Restrict it to the right lines or values.
  • On decimals it misbehaves: 1234.5678 becomes 1,234.5,678. Adding a lookbehind fixes that: (?<!\.\d*)\B(?=(\d{3})+(?!\d)) leaves digits after a decimal point alone. This needs variable-length lookbehind, so use it in .NET or JavaScript.

5. Find lines with one word but not another

With multiline mode on, ^(?!.*draft).*TODO.*$ matches lines that contain TODO but not draft. The lookahead at the start scans the whole line for the word you want to exclude before the rest of the pattern runs.

6. Change a word only in a certain context

Find (?<=class=")old-button(?=") and replace with btn-primary. Only a class attribute containing exactly old-button changes. Text in paragraphs that happens to say old-button is skipped.

7. Skip values that are already formatted

Find (?<!\$)\b(\d+\.\d{2})\b and replace with $$$1 to add a dollar sign to prices that lack one. In .NET and JavaScript replacements, $$ is a literal dollar sign and $1 is the group, so $$$1 turns 9.99 into $9.99 and leaves $4.50 alone.

Combining look-arounds

Look-arounds can be stacked at the same position, and each must succeed. (?<=\s)(?=\d) matches the position between a space and a digit. A common use is checking several conditions on a whole line at once:

  • ^(?=.*\bERROR\b)(?=.*\bdisk\b).*$ matches lines containing both ERROR and disk, in any order.
  • ^(?!.*\S) matches lines that contain no visible characters.

Because look-arounds do not consume text, the order of stacked conditions does not change what matches, only how quickly the engine fails on lines that do not qualify.

Support in Windows tools

Engine / toolLookaheadLookbehind
.NET (PowerShell, many Windows apps)YesYes, any length, including * and +
JavaScript (VS Code)YesYes in modern engines, any length
Boost (Notepad++)YesFixed length only
PCREYesFixed length; alternatives may differ in length
Classic findstrNoNo

"Fixed length" means the engine must know exactly how many characters to step back. (?<=\$) and (?<=<title>) are fixed. (?<=\$\s*) is not, and Notepad++ will reject it. A common workaround there is \K, which drops everything matched so far from the match: \$\s*\K\d+ behaves like a variable-length lookbehind in Boost and PCRE. It is not supported in .NET or JavaScript.

Common mistakes

  • Expecting the look-around text to be replaced. It is not part of the match. If you want it gone, take it out of the look-around.
  • Putting a lookahead in the wrong place. (?=px)\d+ fails, because it asks for px and a digit at the same position. Put the lookahead after the part it describes.
  • Forgetting the escapes. A dollar sign or a dot inside a look-around still needs a backslash: (?<=\$), (?=\.html).
  • Partial-word matches. (?<!re)view still matches inside overview. Add \b where you mean whole words.

The regex cheat sheet has a compact table of look-arounds alongside the other syntax.

Testing look-arounds across a folder

Look-around patterns tend to be precise, which is the point, but a subtle mistake can mean they match nothing or match more than intended. Test on a few sample lines, then run a search-only pass and compare the count with what you expect. Bulk Replace accepts look-arounds in any regex rule and shows a before/after diff of every match before writing, so you can see that 14px changed and 140px did not. Every run is backed up first, so a pattern that turns out to be wrong can be undone in one click.

A good habit with Bulk Replace or any other tool: keep a short text file of the look-around patterns that worked for you, with one example line each. Precise patterns are worth reusing, and the example tells you at a glance what each one was for.

Frequently asked questions

What is the difference between lookahead and lookbehind?

Lookahead (?=...) checks what comes after the current position, lookbehind (?<=...) checks what comes before. Neither becomes part of the match.

What is a negative lookahead?

(?!...) succeeds only if the text that follows does not match. For example \bcolor(?!s) matches color but not colors.

Do all tools support lookbehind?

No. .NET supports variable-length lookbehind and modern JavaScript supports it too, while engines such as PCRE and Boost restrict lookbehind to fixed-length patterns.

Get three guides like this every month, plus software update notices.Subscribe →