This chapter explains proposal “RegExp Named Capture Groups” by Gorkem Yakin, Daniel Ehrenberg.
Before we get to named capture groups, let’s take a look at numbered capture groups; to introduce the idea of capture groups.
Numbered capture groups enable you to take apart a string with a regular expression.
Successfully matching a regular expression against a string returns a match object
matchObj. Putting a fragment of the regular expression in parentheses turns that fragment into a capture group: the part of the string that it matches is stored in
Prior to this proposal, all capture groups were accessed by number: the capture group starting with the first parenthesis via
matchObj, the capture group starting with the second parenthesis via
For example, the following code shows how numbered capture groups are used to extract year, month and day from a date in ISO format:
Referring to capture groups via numbers has several disadvantages:
All issues can be somewhat mitigated by defining constants for the numbers of the capture groups. However, capture groups are an all-around superior solution.
The proposed feature is about identifying capture groups via names:
Here we have tagged the previous capture group #1 with the name
The captured strings are not properties of
matchObj, because you don’t want them to clash with current or future properties created by the regular expression API.
Let’s rewrite the previous code so that it uses named capture groups:
Named capture groups also create indexed entries; as if they were numbered capture groups:
Destructuring can help with getting data out of the match object:
Named capture groups have the following benefits:
You can freely mix numbered and named capture groups.
\k<name> in a regular expression means: match the string that was previously matched by the named capture group
name. For example:
The backreference syntax for numbered capture groups works for named capture groups, too:
You can freely mix both syntaxes:
replace()and named capture groups
The string method
replace() supports named capture groups in two ways.
First, you can mention their names in the replacement string:
Second, each replacement function receives an additional parameter that holds an object with data captured via named groups. For example (line A):
These are the parameters of the callback in line A:
g0contains the whole matched substring,
dare matches for the numbered groups 1–3 (which are created via the named groups
offsetspecifies where the match was found.
inputcontains the complete input string.
day. We use destructuring to access those properties.
The following code shows another way of accessing the last argument:
We receive all arguments via the rest parameter
args. The last element of the Array
args is the object with the data from the named groups. We access it via the index
If an optional named group does not match, its property is set to
undefined (but still exists):
transform-modern-regexpby Dmitry Soshnikov supports named capture groups.
You can check the version of V8 in your Node.js via:
node -p process.versions.v8
In Chrome Canary (60.0+), you can enable named capture groups as follows. First, look up the path of the Chrome Canary binary via the
about: URL. Then start Canary like this (you only need the double quotes if the path contains a space):
$ alias canary='"/tmp/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"' $ canary --js-flags='--harmony-regexp-named-captures'