JavaScript Academy · Lesson

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.

Lesson 2 of 38 steps

Groups, Captures, and Replace is a free JavaScript Academy lesson on CoddyKit. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 3 lessons in total.

Why groups?

Goal: Extract and reshape text.

  • Capture groups: (...)
  • Named groups: (?<name>...)
  • matchAll to loop all results
  • replace with $1 or a function
Groups, Captures, and Replace — illustration 1

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");
}
Groups, Captures, and Replace — illustration 2

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);
}
Groups, Captures, and Replace — illustration 3

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]);
}
Groups, Captures, and Replace — illustration 4

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);
Groups, Captures, and Replace — illustration 5

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.
Groups, Captures, and Replace — illustration 6

matchAll basics quiz

Quick check: Iterate all matches with groups.

Groups, Captures, and Replace — illustration 7

Recap

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

Groups, Captures, and Replace — illustration 8
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Groups, Captures, and Replace” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Groups, Captures, and Replace”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 3 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

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

  1. Regex basics — literals, test, anchors, and flags
  2. Groups, Captures, and Replace
  3. Validation & small pitfalls
← Back to JavaScript Academy