Lookahead
A lookahead checks what comes next without including it in the match. Like anchors, lookarounds are zero-width: they test a condition at the current position and then let the match continue from that same position.
| Syntax | Name | Succeeds when the text ahead… |
|---|---|---|
X(?=Y) |
positive lookahead | …matches Y |
X(?!Y) |
negative lookahead | …does not match Y |
Positive lookahead
Find numbers that are followed by px, but return only the number:
/\d+(?=px)/g
width: 120px; height: 80px; margin: 4em; z: 102 matches
#1 "120" at index 7
#2 "80" at index 22
The px had to be there, but it's not part of the match — handy when you want to replace the number and keep the unit.
Negative lookahead
Numbers not followed by px:
/\b\d+(?!\d|px)/g
width: 120px; margin: 4em; z: 102 matches
#1 "4" at index 22
#2 "10" at index 30
The \d inside the lookahead matters. Without it, \d+ can backtrack to 12 in 120px, where the next character 0 is "not px" — and that counts as a match. Lookarounds interact with backtracking, so always test the edges:
/\d+(?!px)/g
width: 120px1 match
#1 "12" at index 7
Several conditions at once: password rules
Because a lookahead doesn't consume text, you can stack several of them at the start of a pattern. Each one checks a separate rule from the same position:
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?!.*[ \t]).{8,}$/gm
password
Passw0rd
Pass w0rd
Sh0rt
LongEnough92 matches
#1 "Passw0rd" at index 9
#2 "LongEnough9" at index 34
Read it as: at the start of a line, ① somewhere there is a lowercase letter, ② an uppercase letter, ③ a digit, ④ there is no space or tab; then ⑤ match at least 8 characters to the end of the line.
Why [ \t] and not \s? In multiline text .*\s would happily match up to the newline at the end of the line, so every line except the last would be rejected. Little details like this are why testing on realistic input matters.
Note: Composition rules like these are an example, not security advice — modern guidance favours long passphrases and checking against lists of breached passwords over "must contain a digit" rules.
"Not followed by" for words
Find Java when it is not the start of JavaScript:
/\bJava(?!Script)\b/g
Java, JavaScript, Java 212 matches
#1 "Java" at index 0
#2 "Java" at index 18
Matching a line that does NOT contain something
A negative lookahead at every step builds "a line without the word ERROR". (?:(?!ERROR).)* means "characters, each of which is not the start of ERROR":
/^(?:(?!ERROR).)*$/gm
INFO started
ERROR disk full
WARN slow
INFO ERROR retry2 matches
#1 "INFO started" at index 0
#2 "WARN slow" at index 29
Lookahead in code
Insert thousands separators: put a comma at each position that is followed by groups of exactly three digits up to the end of the number.
const addCommas = (n) => String(n).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
console.log(addCommas(1234567));
console.log(addCommas(999));
console.log(addCommas(1000));1,234,567
999
1,000
Only positions are matched (zero characters), so the replacement inserts commas without removing anything. (In real apps, Intl.NumberFormat does this better and respects the user's locale.)
Test yourself
What does /\w+(?=:)/ match in name: Linh?
name:nameLinh:
Which lookahead means "not followed by a digit"?
(?=\d)(?!\d)(?<!\d)[^\d]
Why can you chain several lookaheads at the start of a password pattern?
- They run in parallel threads
- They are zero-width, so each one checks from the same position
- They are greedy
- They capture the password