Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Commit ebe21b5

Browse files
authored
Explainer updates
- Some more details on names - Mark all RegExps as Unicode (but leave in the "Option 3" for disambiguating for now; discussion to continue at #7)
1 parent 166a037 commit ebe21b5

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ Named capture groups provide a nice solution for these issues.
1010

1111
## High Level API
1212

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.
1414

1515
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:
1616

1717
```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;
1919
let result = re.exec('2015-01-02');
2020
// result.year === '2015';
2121
// result.month === '01';
@@ -30,7 +30,7 @@ let result = re.exec('2015-01-02');
3030
The interface interacts nicely with destructuring, as in the following example:
3131

3232
```js
33-
let {one, two} = /^(?<one>.*):(?<two>.*)$/.exec('foo:bar');
33+
let {one, two} = /^(?<one>.*):(?<two>.*)$/u.exec('foo:bar');
3434
console.log(`one: ${one}, two: ${two}`); // prints one: foo, two: bar
3535
```
3636

@@ -57,7 +57,7 @@ duplicate.test('a*a*b'); // false
5757
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:
5858

5959
```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;
6161
let result = '2015-01-02'.replace(re, '$<day>/$<month>/$<year>');
6262
// result === '02/01/2015'
6363
```
@@ -70,7 +70,11 @@ If the second argument to `String.prototype.replace` is a function, then the nam
7070

7171
### Overlapping group names
7272

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.
7478

7579
An alternative possibility would be to provide named matches in a new `groups` object which is a property of the match result object.
7680

0 commit comments

Comments
 (0)