JavaScript Notes
Master JavaScript regex groups: capturing groups (), non-capturing (?:), named groups (?<name>), backreferences, and extracting data from strings. With interview Q&A.
Groups allow you to treat multiple characters as a single unit, capture substrings, and apply quantifiers to patterns. They are the key to extracting specific parts of a matched string.
There are three main types:
- Capturing group
(...)— captures the matched text for later use - Non-capturing group
(?:...)— groups without capturing - Named capturing group
(?<name>...)— captures with a descriptive name
📊 Groups Visual Reference
💻 Example 1 — Capturing Groups
Full match: 2026-06-12 Year: 2026 Month: 06 Day: 12 Username: riya Domain: wohotech.in
💻 Example 2 — Named Capturing Groups
2026 06 12 12/06/2026 12/06/2026
💻 Example 3 — Non-Capturing Groups (?:...)
// Non-capturing: groups without storing the match
// Use when you need grouping (for | or quantifiers) but don't need the captured value
// Alternation with groups: match "color" or "colour"
const colourPattern = /colou?r/; // ? applies only to 'u'
const colourGroup = /colo(?:u)?r/; // same, more explicit with group
console.log(colourPattern.test("color")); // true
console.log(colourGroup.test("colour")); // true
// Difference: capturing vs non-capturing
const capturing = "2026-06-12".match(/(\d{4})-(\d{2})/);
const nonCapturing = "2026-06-12".match(/(?:\d{4})-(?:\d{2})/);
console.log(capturing); // includes group 1, group 2
console.log(nonCapturing); // only full match, no groups
// Protocol matching — group the alternation
const urlPattern = /^(?:https?|ftp):\/\//;
console.log(urlPattern.test("https://wohotech.in")); // true
console.log(urlPattern.test("ftp://files.com")); // true
console.log(urlPattern.test("wohotech.in")); // falsetrue true [ '2026-06', '2026', '06', index: 0, ... ] [ '2026-06', index: 0, ... ] true true false
💻 Example 4 — Groups in replace() with References
// Capture groups in replace — use $1, $2 to reference groups
const name = "Sharma, Riya";
// Reformat "Last, First" → "First Last"
const reformatted = name.replace(/^(\w+),\s*(\w+)$/, "$2 $1");
console.log(reformatted);
// Mask credit card — keep last 4 digits
const card = "1234-5678-9012-3456";
const masked = card.replace(/(\d{4}-){3}(\d{4})/, "****-****-****-$2");
console.log(masked);
// Wrap words with <strong>
const text = "JavaScript is awesome";
const wrapped = text.replace(/\b(\w+)\b/g, "<strong>$1</strong>");
console.log(wrapped);Riya Sharma ****-****-****-3456 <strong>JavaScript</strong> <strong>is</strong> <strong>awesome</strong>
💻 Example 5 — Alternation with Groups: (a|b)
https://wohotech.in: true ftp://files.com: true file:///local: true ws://socket.io: false +91 9876543210: true +1 1234567890: true +99 1234567890: false
📋 Group Types Quick Reference
| Syntax | Type | Captures? | Use Case | |
|---|---|---|---|---|
(abc) | Capturing | ✅ Yes | Extract data from matches | |
(?:abc) | Non-capturing | ❌ No | Grouping without storing | |
(?<name>abc) | Named capturing | ✅ Named | Self-documenting captures | |
| `(a\ | b)` | Alternation | ✅ Yes | Match one of several options |
⚠️ Common Mistakes
❌ Mistake 1 — Group Index Off by One
hello world hello world
❌ Mistake 2 — Using Capturing Groups When You Don't Need Captures
// BAD — creates unnecessary captures, polluting match array
const bad = /^(https?):(\/\/)/;
const m = "https://wohotech.in".match(bad);
console.log(m); // extra capture groups you didn't need
// GOOD — use non-capturing for structure-only groups
const good = /^(?:https?):(?:\/\/)/;❌ Mistake 3 — Forgetting g Flag with match() Loses Group Info
[ '2026-01-15', '2026-06-12' ] 2026 01 15 2026 06 12
🎯 Key Takeaways
(...)captures the matched text — accessible viamatch[1],match[2], etc.(?:...)groups without capturing — use for alternation or quantifiers without storing(?<name>...)named groups — accessible viamatch.groups.name(clearer than numbered)- In
replace(), reference captures with$1,$2or$<name>for named groups - With the
gflag,match()returns all full matches but drops group captures — usematchAll()instead - Group indexes start at 1 — index
0is always the full match
❓ Interview Questions
Q1. What is a capturing group in regex? > A capturing group (...) matches the enclosed pattern and stores the matched text. You access it via result[1], result[2], etc. in the match array. Index 0 is always the full match.
Q2. What is a non-capturing group (?:...)? > A non-capturing group groups the pattern for alternation or quantifiers without storing the matched text. It reduces memory overhead and avoids polluting the match array when you don't need the captured value.
Q3. What is a named capturing group? > (?<name>pattern) captures and labels the match with a name. Access it via result.groups.name. More readable than numbered groups, especially in complex patterns. Referenced in replace() as $<name>.
Q4. How do you use capture group values in replace()? > Use $1, $2, etc. for numbered groups or $<name> for named groups in the replacement string. Example: "Sharma, Riya".replace(/^(\w+),\s*(\w+)$/, "$2 $1") → "Riya Sharma".
Q5. Why does match() with the g flag lose group information? > With g, match() returns an array of all full match strings. Capturing group data is only available in non-global matches or when using exec() in a loop or matchAll(), which yields each match object with full group information.
Q6. How is matchAll() different from match()? > matchAll() returns an iterator of all match objects (each with full group info) — like running exec() in a loop. match() with g returns only the array of full matches, losing group captures.
Q7. What is alternation in groups — (a|b)? > The | inside a group means "or" — match either the left or right alternative. (cat|dog) matches "cat" or "dog". Without grouping, cat|dog has lower precedence — use groups to scope alternation correctly.
Q8. How many capturing groups can a regex have? > Practically unlimited. Each () increments the group counter. Named groups must have unique names within the same pattern. Excessive groups slow down the regex engine — use non-capturing (?:...) when capture is not needed.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Regex Groups – Capturing, Non-Capturing & Named Groups.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, regex, groups, javascript regex groups – capturing, non-capturing & named groups
Related JavaScript Master Course Topics