Lookahead & Lookbehind Assertions0%

Lookahead & Lookbehind Assertions

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

Lookahead & Lookbehind Assertions in Modern JavaScript

Regular expressions are central to input validation and string manipulation. Advanced pattern matching often requires verifying what precedes or follows a pattern without actually including those characters in the matched result. These zero-width assertions are known as Lookahead and Lookbehind assertions.


1. The 4 Lookaround Assertions

TypeSyntaxMeaningExample Match
Positive LookaheadX(?=Y)Match X only if followed immediately by Y/\d+(?=px)/ matches 400 in "400px"
Negative LookaheadX(?!Y)Match X only if NOT followed by Y/\d+(?!px)/ matches 400 in "400kg"
Positive Lookbehind(?<=Y)XMatch X only if preceded immediately by Y/(?<=\$)\d+/ matches 50 in "$50"
Negative Lookbehind(?<!Y)XMatch X only if NOT preceded by Y/(?<!\$)\d+/ matches 50 in "€50"
Zero-Width Guarantee: Lookarounds are zero-width: they test conditions in the text, but do not consume or include the matched lookaround characters in the returned match string!

2. Practical Examples

1. Extracting Monetary Amounts without Currency Symbols

JavaScript
const invoice = "Prices: $120, €45, ¥3000, $500";
 
// Positive Lookbehind: Match digits preceded by $
const usdPrices = invoice.match(/(?<=\$)\d+/g);
console.log(usdPrices); // ['120', '500'] (Dollar signs are NOT in results!)

2. Matching File Names by Extension

JavaScript
const files = "photo.png, invoice.pdf, backup.zip, avatar.png";
 
// Positive Lookahead: Find base name of png files
const pngBases = files.match(/\b\w+(?=\.png)/g);
console.log(pngBases); // ['photo', 'avatar']

3. The Power of Multiple Lookaheads: Password Validation

In password validation, requiring at least one lowercase letter, one uppercase letter, one digit, and a minimum length is cumbersome with standard regex. Chained Lookaheads validate all conditions simultaneously from the start of the string:

JavaScript
function validateEnterprisePassword(password) {
// (?=.*[a-z]) -> Must contain lowercase
// (?=.*[A-Z]) -> Must contain uppercase
// (?=.*\d) -> Must contain number
// (?=.*[@$!%*?&]) -> Must contain special character
// [A-Za-z\d@$!%*?&]{8,} -> Minimum 8 chars
const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
return regex.test(password);
}
 
console.log(validateEnterprisePassword('Weak123')); // false (No special char, < 8)
console.log(validateEnterprisePassword('SuperSecret123!')); // true

4. Lookbehind Browser Compatibility (ES2018)

Positive lookbehind (?<=...) and negative lookbehind (?<!...) were formalized in ES2018 and are supported across all modern browsers and Node.js environments.


Interactive Quiz

Practice Quiz

Question 1
What does the positive lookahead syntax X(?=Y) assert?
Question 2
What will "100px 200em 300px".match(/\d+(?=px)/g) return?
Question 3
Which lookaround assertion tests that a pattern is NOT preceded by a specific string?
Question 4
Why are Lookarounds termed "Zero-Width Assertions"?
Question 5
How do multiple lookaheads at the start of a regex (/^(?=.[A-Z])(?=.\d)/) behave?
Next Lesson

Named Capture Groups & Modern Regex Flags

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext