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.
(?<name>pattern)Assign semantic identifiers to captured sub-patterns:
replace() & replaceAll()Named groups can be referenced in replacement strings using $<name>:
| Flag | Name | Purpose | Example |
|---|---|---|---|
d (ES2022) | Indices | Generates exact start/end match slice indices in match.indices | /foo/d.exec('foo').indices |
s (ES2018) | dotAll | Allows the dot . to match newline characters (\n, \r) | /start.*end/s |
u (ES2015) | Unicode | Treats pattern as a sequence of Unicode code points (handles emojis) | /\u{1F600}/u |
v (ES2024) | UnicodeSets | Advanced set operations (intersection &&, subtraction --) | [[a-z]--[b-d]]/v |
y (ES2015) | Sticky | Matches strictly from regex.lastIndex without searching forward | /abc/y |
d Flag (Match Indices):s (dotAll) Flag:Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Lookahead & Lookbehind Assertions | Regex Denial of Service (ReDoS) Prevention |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.