大网数据广告活动
Web 标准与前端开发

Named capturing group: (?<name>...)

· 52CE 我爱测

A **named capturing group** is a particular kind of capturing group that allows you to give a name to the group. The group's matching result can later be identified by this name instead of by its index in the pattern.

A named capturing group is a particular kind of capturing group that allows you to give a name to the group. The group's matching result can later be identified by this name instead of by its index in the pattern.

Syntax

(?<name>pattern)

Parameters

  • pattern
    • : A pattern consisting of anything you may use in a regex literal, including a disjunction.
  • name
    • : The name of the group. Must be a valid identifier.

Description

Named capturing groups can be used just like capturing groups — they also have their match index in the result array, and they can be referenced through \1, \2, etc. The only difference is that they can be additionally referenced by their name. The information of the capturing group's match can be accessed through:

  • The groups property of the return value of RegExp.prototype.exec(), or String.prototype.match() when the regex does not have the g flag
  • The groups property of each match array yielded by the iterator returned by String.prototype.matchAll()
  • The groups parameter of the String.prototype.replace() and String.prototype.replaceAll() methods' replacement callback function
  • Named backreferences within the same pattern

All names must be unique within the same pattern. Multiple named capturing groups with the same name result in a syntax error.

/(?<name>)(?<name>)/; // SyntaxError: Invalid regular expression: Duplicate capture group name

This restriction is relaxed if the duplicate named capturing groups are not in the same disjunction alternative, so for any string input, only one named capturing group can actually be matched. This is a much newer feature, so check browser compatibility before using it.

/(?<year>\d{4})-\d{2}|\d{2}-(?<year>\d{4})/;
// Works; "year" can either come before or after the hyphen

Named capturing groups will all be present in the result. If a named capturing group is not matched (for example, it belongs to an unmatched alternative in a disjunction), the corresponding property on the groups object has value undefined.

/(?<ab>ab)|(?<cd>cd)/.exec("cd").groups; // [Object: null prototype] { ab: undefined, cd: 'cd' }

You can get the start and end indices of each named capturing group in the input string by using the d flag. In addition to accessing them on the indices property on the array returned by exec(), you can also access them by their names on indices.groups.

Compared to unnamed capturing groups, named capturing groups have the following advantages:

  • They allow you to provide a descriptive name for each submatch result.
  • They allow you to access submatch results without having to remember the order in which they appear in the pattern.
  • When refactoring code, you can change the order of capturing groups without worrying about breaking other references.

Examples

Using named capturing groups

The following example parses a timestamp and an author name from a Git log entry (output with git log --format=%ct,%an -- filename):

function parseLog(entry) {
  const { author, timestamp } = /^(?<timestamp>\d+),(?<author>.+)$/.exec(
    entry,
  ).groups;
  return `${author} committed on ${new Date(
    parseInt(timestamp, 10) * 1000,
  ).toLocaleString()}`;
}

parseLog("1560979912,Caroline"); // "Caroline committed on 6/19/2019, 5:31:52 PM"

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。