MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Lookahead & Lookbehind Assertions0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
14.1Lookahead & Lookbehind Assertions14.2Named Capture Groups & Modern Regex Flags14.3Regex Denial of Service (ReDoS) Prevention14.4Advanced Pattern Matching & Replacement
HomeTutorialsJavaScript for AdvancedLookahead & Lookbehind Assertions
Named Capture Groups & Modern Regex Flags

Lookahead & Lookbehind Assertions

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

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

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

2. Matching File Names by Extension

1const files = "photo.png, invoice.pdf, backup.zip, avatar.png";
2 
3// Positive Lookahead: Find base name of png files
4const pngBases = files.match(/\b\w+(?=\.png)/g);
5console.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:

1function validateEnterprisePassword(password) {
2 // (?=.*[a-z]) -> Must contain lowercase
3 // (?=.*[A-Z]) -> Must contain uppercase
4 // (?=.*\d) -> Must contain number
5 // (?=.*[@$!%*?&]) -> Must contain special character
6 // [A-Za-z\d@$!%*?&]{8,} -> Minimum 8 chars
7 const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
8 return regex.test(password);
9}
10 
11console.log(validateEnterprisePassword('Weak123')); // false (No special char, < 8)
12console.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• Knowledge Check

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.

Related Lessons

Previous LessonNext Lesson
Core Web Vitals & JavaScript ImpactNamed Capture Groups & Modern Regex Flags

Practice Quiz

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

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext