Level 2: Practical patterns · Lesson 17 of 30
Lookahead
Lookahead checks what comes next without consuming it. Use (?=...) to require something after a match and (?!...) to forbid it.
Most regex tokens consume characters: once \d matches a digit, that digit is part of the match and the engine moves past it. A lookahead is different. It peeks at the text ahead of the current position, succeeds or fails, and then returns to where it started. Nothing it looks at becomes part of the match.
Positive lookahead is written (?=...). The pattern \d+(?= USD) matches a number only if it is directly followed by a space and USD. In the text 250 USD and 15 EUR the full match is 250, not 250 USD. The currency is required but not included, which is exactly what you want when extracting the amount alone.
Negative lookahead is written (?!...). It succeeds only when the inner pattern does not match at that position. The pattern \b[A-Z]{3}\b(?!-OLD) matches three-letter codes that are not followed by -OLD. Negative lookahead is the regex way of saying 'but not if'.
Because lookaheads do not move the position, you can stack several of them at the same spot. A classic use is checking multiple rules at once: \b(?=\w*\d)\w{6,}\b first confirms that somewhere ahead there is a digit, and then matches a word of six or more characters. Each lookahead is an independent condition tested from the same starting point.
Watch out for backtracking around negative lookahead. If you write \w+(?!\.tmp) hoping to skip cache.tmp, the engine can simply give back one letter, match cach, and find that the next characters e.tmp are not .tmp. Adding a word boundary, as in \b\w+\b(?!\.tmp), forces the word to end before the check runs, so the trick no longer works.
Lookahead is supported in JavaScript, Python, PHP, Java, C# and Ruby. It is not supported in Go or Rust, whose engines guarantee linear-time matching and therefore leave out lookarounds. If your pattern must run in those languages, match the extra text and trim it in code, or use a capture group instead.
In practice tasks, remember that only the full match is checked. A lookahead lets you make the full match exactly the part you want while still insisting on the surrounding context.
Live tester
2 matches (JavaScript engine)
Price 250 USD, shipping 15 EUR, tax 30 USD.
Examples
Numbers
/\d+(?=%)/Growth 12% and 7 points, margin 35%.
Matches: “12”, “35”
Only numbers directly followed by a percent sign match, and the sign stays outside the match.
Text
/\b\w+(?=ing\b)/We are testing and reading the log.
Matches: “test”, “read”
The word stem matches when `ing` follows at the end of the word, giving `test` and `read`.
Codes
/\b[A-Z]{3}\b(?!-OLD)/Codes ABC, DEF-OLD, GHI are listed.
Matches: “ABC”, “OLD”, “GHI”
Negative lookahead rejects any code that is followed by `-OLD`.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match only the numbers that are followed by `kg`. Do not include the unit.
Bag 20kg, box 15 lb, crate 40kg
Task 2: Match the base names of files whose extension is not `.tmp`.
report.pdf cache.tmp invoice.pdf temp.tmp
Task 3: Match whole words of at least six characters that contain at least one digit.
alpha beta7x gamma12 delta