X Xerobit

Regex Named Capture Groups — ?<name> Syntax and Use Cases

Named capture groups in regex use (?<name>...) syntax to give match groups readable names instead of positional indexes. Learn named groups in JavaScript, Python, and practical...

Mian Ali Khalid · · 4 min read
Use the tool
Regex Tester
Test regular expressions with live match highlighting and explanation.
Open Regex Tester →

Named capture groups let you reference matched groups by name instead of [1], [2] index. This makes complex regexes readable and resilient to group order changes.

Use the Regex Tester to test named group patterns.

Syntax

// Numbered groups (hard to read):
const match = '2026-05-11'.match(/(\d{4})-(\d{2})-(\d{2})/);
const year  = match[1];
const month = match[2];
const day   = match[3];

// Named groups (clear and readable):
const match = '2026-05-11'.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);
const { year, month, day } = match.groups;
// year='2026', month='05', day='11'

Date parsing

const DATE_REGEX = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;

function parseDate(dateStr) {
  const match = dateStr.match(DATE_REGEX);
  if (!match) return null;
  const { year, month, day } = match.groups;
  return new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
}

parseDate('2026-05-11')  // Date object for May 11, 2026

// Multiple date formats:
const FLEXIBLE_DATE = /(?<year>\d{4})[-\/](?<month>\d{1,2})[-\/](?<day>\d{1,2})/;
const match = '2026/5/11'.match(FLEXIBLE_DATE);
// { year: '2026', month: '5', day: '11' }

URL parsing

const URL_REGEX = /(?<protocol>https?):\/\/(?<hostname>[^/:]+)(?::(?<port>\d+))?(?<path>\/[^?#]*)?(?:\?(?<query>[^#]*))?(?:#(?<fragment>.*))?/;

const match = 'https://api.example.com:8080/v2/users?page=2#results'.match(URL_REGEX);
const { protocol, hostname, port, path, query, fragment } = match.groups;
// protocol='https', hostname='api.example.com', port='8080'
// path='/v2/users', query='page=2', fragment='results'

Log parsing

// Apache/Nginx access log format:
// 192.168.1.1 - - [11/May/2026:10:30:00 +0000] "GET /api/users HTTP/1.1" 200 1234

const LOG_REGEX = /^(?<ip>[\d.]+) \S+ \S+ \[(?<time>[^\]]+)\] "(?<method>\w+) (?<path>[^ ]+) HTTP\/[\d.]+" (?<status>\d+) (?<size>\d+)/;

function parseAccessLog(line) {
  const match = line.match(LOG_REGEX);
  return match ? match.groups : null;
}

const entry = parseAccessLog('192.168.1.1 - - [11/May/2026:10:30:00 +0000] "GET /api/users HTTP/1.1" 200 1234');
// { ip: '192.168.1.1', method: 'GET', path: '/api/users', status: '200', ... }

Named backreferences

Named groups can be referenced later in the same pattern using \k<name>:

// Match repeated words (using named backreference):
/\b(?<word>\w+)\s+\k<word>\b/.test('the the')   // true (repeated "the")
/\b(?<word>\w+)\s+\k<word>\b/.test('hello world') // false

// Extract repeated HTML tag pairs:
/<(?<tag>[a-z]+)>[^<]*<\/\k<tag>>/i.test('<b>text</b>')  // true
/<(?<tag>[a-z]+)>[^<]*<\/\k<tag>>/i.test('<b>text</i>')  // false (mismatched)

Replace with named groups

// Rearrange date format using named groups in replace:
'2026-05-11'.replace(
  /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/,
  '$<month>/$<day>/$<year>'
)
// '05/11/2026'

// Or with a function:
'2026-05-11'.replace(
  /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/,
  (_, year, month, day, offset, str, groups) => {
    return `${groups.month}/${groups.day}/${groups.year}`;
  }
)
// '05/11/2026'

Python named groups

import re

# Same syntax as JavaScript: (?P<name>...) — older Python style
# Modern Python also supports (?<name>...) in Python 3.12+

DATE_PATTERN = re.compile(r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})')

match = DATE_PATTERN.match('2026-05-11')
year  = match.group('year')   # '2026'
month = match.group('month')  # '05'

# Or via groupdict():
data = match.groupdict()  # {'year': '2026', 'month': '05', 'day': '11'}

# Replace with named groups:
result = re.sub(
    r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})',
    r'\g<month>/\g<day>/\g<year>',
    '2026-05-11'
)
# '05/11/2026'

Related posts

Related tool

Regex Tester

Test regular expressions with live match highlighting and explanation.

Written by Mian Ali Khalid. Part of the Dev Productivity pillar.