You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 25, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+9-5Lines changed: 9 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,12 +10,12 @@ Named capture groups provide a nice solution for these issues.
10
10
11
11
## High Level API
12
12
13
-
A capture group can be given a name using the `(?<name>...)` syntax, for any identifer`name`. The regular expression for a date then can be written as `/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/`. Each name should be unique and follow the grammar for ECMAScript identifiers.
13
+
A capture group can be given a name using the `(?<name>...)` syntax, for any identifier`name`. The regular expression for a date then can be written as `/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u`. Each name should be unique and follow the grammar for ECMAScript IdentifierName.
14
14
15
15
Named groups can be accessed from properties of the regular expression result. Numbered references to the groups are also created, just as for non-named groups. For example:
16
16
17
17
```js
18
-
let re =/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
18
+
let re =/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u;
19
19
let result =re.exec('2015-01-02');
20
20
// result.year === '2015';
21
21
// result.month === '01';
@@ -30,7 +30,7 @@ let result = re.exec('2015-01-02');
30
30
The interface interacts nicely with destructuring, as in the following example:
31
31
32
32
```js
33
-
let {one, two} =/^(?<one>.*):(?<two>.*)$/.exec('foo:bar');
33
+
let {one, two} =/^(?<one>.*):(?<two>.*)$/u.exec('foo:bar');
34
34
console.log(`one: ${one}, two: ${two}`); // prints one: foo, two: bar
Named groups can be referenced from the replacement value passed to `String.prototype.replace` too. If the value is a string, named groups can be accessed using the `$<name>` syntax. For example:
58
58
59
59
```js
60
-
let re =/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
60
+
let re =/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u;
61
61
let result ='2015-01-02'.replace(re, '$<day>/$<month>/$<year>');
62
62
// result === '02/01/2015'
63
63
```
@@ -70,7 +70,11 @@ If the second argument to `String.prototype.replace` is a function, then the nam
70
70
71
71
### Overlapping group names
72
72
73
-
RegExp result objects have some non-numerical properties already, which named capture groups may overlap with, namely `length`, `index` and `input`. In this proposal, to avoid ambiguity and edge cases around overlapping names, RegExps with such properties would be an Early Error, rejected when the RegExp is parsed.
73
+
RegExp result objects have some non-numerical properties already, which named capture groups may overlap with, namely `length`, `index` and `input`. In this proposal, to avoid ambiguity and edge cases around overlapping names, RegExps with such properties would be an Early Error, rejected when the RegExp is parsed. The grammatical production would be similar to `Identifier`, except leaving out these three names rather than `ReservedWords`.
74
+
75
+
The hazard with this option is that it does not scale to adding more ordinary properties to the output of `exec`. However, there are a few reasons why this is unlikely to happen:
76
+
- ES2015 makes `exec` a protocol which other RegExp subclasses/analogues can implement. Adding another expected output would be a change in the protocol.
77
+
- It would be hard to add a new required output without producing a difficult-to-avoid performance degradation; therefore, additional pieces of information might be better added through other APIs.
74
78
75
79
An alternative possibility would be to provide named matches in a new `groups` object which is a property of the match result object.
0 commit comments