Regexdrill

Groups and lookarounds Updated November 2026

Named groups (?<name>...)

A named group (?<name>...) captures text under a name instead of a number: (?<year>\d{4})-(?<month>\d{2}) gives match.groups.year and match.groups.month in JavaScript, $<year> in replacements and \k<year> as a backreference. Python uses (?P<name>...) and m.group(name); .NET, PCRE and Java accept (?<name>...).

Syntax

(?<name>pattern)      match.groups.name      $<name>      \k<name>      Python: (?P<name>pattern)

Examples

"2024-03-15".match(/(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/).groups

Result: { y: "2024", m: "03", d: "15" }

"2024-03-15".replace(/(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/, "$<d>/$<m>/$<y>")

Result: "15/03/2024"

const { user, host } = "jane@example.com".match(/(?<user>[^@]+)@(?<host>.+)/).groups;
[user, host]

Result: ["jane", "example.com"]

Destructure groups straight into variables.

/^(?<q>["']).*\k<q>$/.test("'quoted'")

Result: true

\k<q> requires the same quote character at the end.

Common mistakes

  • Using Python syntax (?P<name>...) in JavaScript, or (?<name>...) in Python's re module; each throws a syntax error in the other.
  • Reusing a name twice in one pattern; it is an error (JavaScript allows duplicates only in separate alternatives since ES2025).
  • Reading match.groups when the pattern has no named groups; it is undefined, not an empty object.
  • Using a hyphen or space in a name; names must be valid identifiers such as first_name.

Practice Named groups (?<name>...)

These drills check your answer on live data, free, no account needed.

  • Named groups: year, month, dayFind the first date written YYYY-MM-DD and capture its parts in named groups called year, month and day.
  • Dates YYYY-MM-DDThe whole line must match. Match a date written YYYY-MM-DD with a month from 01 to 12 and a day from 01 to 31. Do not worry about month lengths.
  • Reformat MM/DD/YYYY as YYYY-MM-DDRewrite every date written MM/DD/YYYY as YYYY-MM-DD. Leave everything else unchanged.

Questions

How do I use named groups in JavaScript?

Write (?<name>...) in the pattern, then read match.groups.name. In replace, refer to it as $<name>; in a replacer function it is the last argument (an object).

How do named groups work in Python?

Write (?P<name>...) and read m.group("name") or m.groupdict(). In re.sub replacements use \g<name>.

Can I mix named and numbered groups?

Yes. Named groups also get a number in order of appearance, so match[1] and match.groups.year refer to the same text.

Related

  • Capture groupsHow parentheses capture text, reading groups in JavaScript via match, matchAll and exec, reusing them with $1 in replace, and the g trap that loses groups.
  • Backreferences \1What \1 means, finding doubled letters and repeated words, matching a closing quote equal to the opening one, and \1 in a pattern versus $1 in a replacement.
  • Regex for date YYYY-MM-DDA regex for ISO dates like 2024-03-15 with month and day ranges, named groups for year, month and day, reformatting MM/DD/YYYY, and what a regex cannot check.
  • Regex in Python (re)How the Python re module works: search versus match versus fullmatch, findall and its group behavior, sub for replacement, raw strings, and flags.

Previous: Non-capturing group (?:...)Next: Backreferences \1