Groups in JavaScript

From the JavaScript Regular Expressions cheat sheet · Groups & References · verified Jul 2026

Groups

Grouping patterns and capturing matches

javascript
(x)         Capturing group
(?:x)       Non-capturing group
(?<name>x)  Named capturing group
\1, \2      Backreference to group 1, 2
\k<name>    Backreference to named group

// Examples
const match = "John Smith".match(/(\w+) (\w+)/)
// match[0]: "John Smith" (full match)
// match[1]: "John" (group 1)
// match[2]: "Smith" (group 2)

// Named groups
const regex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
const result = "2024-03-15".match(regex)
// result.groups.year: "2024"
// result.groups.month: "03"
// result.groups.day: "15"

// Non-capturing group (for grouping only)
/(?:https?):\/\//.test("https://")  // true
// Won't create a capture group

// Backreferences
/(\w+) \1/.test("hello hello")      // true (repeated word)
/(['"])(.*?)\1/.test('"text"')      // true (matching quotes)
💡 Use (?:) when you need grouping but not capturing
⚡ Named groups make code more readable and maintainable
📌 Backreferences match the exact same text, not pattern
🟢 Groups are numbered from left to right by opening paren
groupscapturing

Continue with JavaScript Regular Expressions

Save the full cheat sheet or work through every related task.

More JavaScript tasks

Back to the full JavaScript Regular Expressions cheat sheet