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
Named Capture Groups & Modern Regex Flags0%
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 AdvancedNamed Capture Groups & Modern Regex Flags
Regex Denial of Service (ReDoS) Prevention

Named Capture Groups & Modern Regex Flags

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

Traditional regular expressions rely on numeric indices (match[1], match[2]) to extract captured substrings. As regex patterns grow complex, numeric indexing becomes fragile and unreadable. Modern ECMAScript introduced Named Capture Groups ((?<name>...)) and modern flags (d, s, u, v) to make pattern matching self-documenting and Unicode-compliant.


1. Named Capture Groups: (?<name>pattern)

Assign semantic identifiers to captured sub-patterns:

1// Traditional Numeric Capture (Fragile):
2// const match = text.match(/(\d{4})-(\d{2})-(\d{2})/);
3// const year = match[1];
4 
5// Modern Named Capture Groups:
6const datePattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
7const match = datePattern.exec('Release Date: 2026-09-15');
8 
9console.log(match.groups);
10// { year: "2026", month: "09", day: "15" }
11 
12// Clean Object Destructuring:
13const { year, month, day } = match.groups;
14console.log(`Formatted: ${day}/${month}/${year}`); // "15/09/2026"

2. Using Named Groups in replace() & replaceAll()

Named groups can be referenced in replacement strings using $<name>:

1const isoDate = '2026-12-25';
2 
3// Reformat YYYY-MM-DD into DD.MM.YYYY using named groups:
4const europeanDate = isoDate.replace(
5 /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/,
6 '$<day>.$<month>.$<year>'
7);
8 
9console.log(europeanDate); // "25.12.2026"

3. Modern Regex Flags Deep Dive

FlagNamePurposeExample
d (ES2022)IndicesGenerates exact start/end match slice indices in match.indices/foo/d.exec('foo').indices
s (ES2018)dotAllAllows the dot . to match newline characters (\n, \r)/start.*end/s
u (ES2015)UnicodeTreats pattern as a sequence of Unicode code points (handles emojis)/\u{1F600}/u
v (ES2024)UnicodeSetsAdvanced set operations (intersection &&, subtraction --)[[a-z]--[b-d]]/v
y (ES2015)StickyMatches strictly from regex.lastIndex without searching forward/abc/y

The d Flag (Match Indices):

1const regex = /(?<word>\w+)/d;
2const res = regex.exec('Hello World');
3 
4console.log(res.indices.groups.word); // [0, 5] (Start index: 0, End index: 5!)

The s (dotAll) Flag:

1const text = `Title: Introduction\nContent: First Chapter\nFooter: End`;
2 
3// Without 's', dot '.' fails across newlines!
4// With 's', '.' matches EVERYTHING including newlines:
5const matched = text.match(/Title:.*Footer/s);
6console.log(matched[0]); // Matches entire multi-line block!

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What syntax declares a Named Capture Group in JavaScript Regular Expressions?
Question 2
How can you reference a named capture group inside a string replace() replacement pattern?
Question 3
What does the s (dotAll) flag enable in regular expressions?
Question 4
What feature was introduced by the d flag in ES2022?
Question 5
If a regex with named groups does NOT match, what does match.groups evaluate to?
Next Lesson

Regex Denial of Service (ReDoS) Prevention

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

Related Lessons

Previous LessonNext Lesson
Lookahead & Lookbehind AssertionsRegex Denial of Service (ReDoS) Prevention

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