Groups, Captures, and Replace
Capture parts of text with groups, use named groups, iterate all matches, and perform safe replacements with backreferences or a function.
Groups, Captures, and Replace is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why groups?
Goal: Extract and reshape text.
- Capture groups: (...)
- Named groups: (?<name>...)
- matchAll to loop all results
- replace with $1 or a function

Numbered groups
Use (...) to capture parts. Access them by index: m[1], m[2], ...
// Capture YYYY-MM-DD into groups
const r = /(\\d{4})-(\\d{2})-(\\d{2})/;
const m = r.exec("Today: 2025-09-26");
if (m) {
console.log("year:", m[1], "month:", m[2], "day:", m[3]);
} else {
console.log("no match");
}

Named groups
Named groups add readable keys via m.groups.name.
// Named groups make code clearer
const rx = /(?<y>\\d{4})-(?<m>\\d{2})-(?<d>\\d{2})/;
const hit = rx.exec("Release 2024-01-05");
if (hit) {
console.log("y:", hit.groups.y, "m:", hit.groups.m, "d:", hit.groups.d);
}

Iterate matches
matchAll returns an iterator; use it with g to loop every match and read groups.
// matchAll with the g flag to get every match and groups
const text = "IDs: A-12, B-7, C-300";
const pattern = /([A-Z])-(\\d+)/g;
for (const match of text.matchAll(pattern)) {
console.log("letter:", match[1], "num:", match[2]);
}

Replace with groups
replace can use $1, $2 or a callback to build the new text.
// Replace order: "2025-09-26" -> "26/09/2025"
const date = "2025-09-26";
// With backreferences
const swapped1 = date.replace(/(\\d{4})-(\\d{2})-(\\d{2})/, "$3/$2/$1");
console.log("swap1:", swapped1);
// With a function for more control
const swapped2 = date.replace(/(?<y>\\d{4})-(?<m>\\d{2})-(?<d>\\d{2})/, function (_all, _y, _m, _d, groups) {
return groups.d + "/" + groups.m + "/" + groups.y;
});
console.log("swap2:", swapped2);

Tips & pitfalls
Tips:
- Add g when you need all matches (use matchAll).
- Prefer named groups for clarity.
- Keep patterns small; test on short sample strings.

matchAll basics quiz
Quick check: Iterate all matches with groups.

Recap
Recap: Capture with (...), use named groups for clarity, loop all matches via matchAll, and reshape text using replace with backreferences or a callback.

Frequently asked questions
Is the “Groups, Captures, and Replace” lesson free?
Yes — the full text of “Groups, Captures, and Replace” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Groups, Captures, and Replace”?
Capture parts of text with groups, use named groups, iterate all matches, and perform safe replacements with backreferences or a function. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Groups, Captures, and Replace” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Regex basics — literals, test, anchors, and flags
- Groups, Captures, and Replace
- Validation & small pitfalls