Skip to content

Commit 3d1d364

Browse files
Update README
1 parent 36ae4da commit 3d1d364

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

README.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ Below are the AST node types for different regular expressions patterns:
275275
- [Disjunction](#disjunction)
276276
- [Groups](#groups)
277277
- [Capturing group](#capturing-group)
278+
- [Named capturing group](#named-capturing-group)
278279
- [Non-capturing group](#non-capturing-group)
279280
- [Backreferences](#backreferences)
280281
- [Quantifiers](#quantifiers)
@@ -294,6 +295,8 @@ Below are the AST node types for different regular expressions patterns:
294295
- [Positive lookahead assertions](#positive-lookahead-assertions)
295296
- [Negative lookahead assertions](#negative-lookahead-assertions)
296297
- [Lookbehind assertions](#lookbehind-assertions)
298+
- [Positive lookbehind assertions](#positive-lookbehind-assertions)
299+
- [Negative lookbehind assertions](#negative-lookbehind-assertions)
297300

298301
#### Char
299302

@@ -711,6 +714,39 @@ Another example:
711714
(5|[a-z])
712715
```
713716

717+
##### Named capturing group
718+
719+
> NOTE: _Named capturing groups_ are not yet supported by JavaScript RegExp. It is an ECMAScript [proposal](https://tc39.github.io/proposal-regexp-named-groups/) which is at stage 3 at the moment.
720+
721+
A capturing group can be given a name using the `(?<name>...)` syntax, for any identifier `name`.
722+
723+
For example, a regular expressions for a date:
724+
725+
```js
726+
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u
727+
```
728+
729+
For the group:
730+
731+
```js
732+
(?<foo>x)
733+
```
734+
735+
We have the following node (the `name` property with value `foo` is added):
736+
737+
```js
738+
{
739+
type: 'Group',
740+
capturing: true,
741+
name: 'foo',
742+
expression: {
743+
type: 'Char',
744+
value: 'x',
745+
kind: 'simple'
746+
}
747+
}
748+
```
749+
714750
##### Non-capturing group
715751
716752
Sometimes we don't need to actually capture the matched string from a group. In this case we can use a _non-capturing_ group:
@@ -757,7 +793,7 @@ The same node, the `capturing` flag is `false`:
757793
758794
##### Backreferences
759795
760-
A captured group can be referenced in the pattern using notation of an escaped group number.
796+
A [capturing group](#capturing-group) can be referenced in the pattern using notation of an escaped group number.
761797
762798
Matches `abab` string:
763799
@@ -792,12 +828,54 @@ A node:
792828
},
793829
{
794830
type: 'Backreference',
831+
kind: 'number',
832+
number: 1,
795833
reference: 1,
796834
}
797835
]
798836
}
799837
```
800838
839+
A [named capturing group](#named-capturing-group) can be accessed using `\k<name>` pattern, and also using a numbered reference.
840+
841+
Matches `www`:
842+
843+
```js
844+
(?<foo>w)\k<foo>\1
845+
```
846+
847+
A node:
848+
849+
```js
850+
{
851+
type: 'Alternative',
852+
expressions: [
853+
{
854+
type: 'Group',
855+
capturing: true,
856+
name: 'foo',
857+
expression: {
858+
type: 'Char',
859+
value: 'w',
860+
kind: 'simple'
861+
}
862+
},
863+
{
864+
type: 'Backreference',
865+
kind: 'name',
866+
number: 1,
867+
reference: 'foo'
868+
},
869+
{
870+
type: 'Backreference',
871+
kind: 'number',
872+
number: 1,
873+
reference: 1
874+
}
875+
]
876+
}
877+
```
878+
801879
#### Quantifiers
802880
803881
Quantifiers specify _repetition_ of a regular expression (or of its part). Below are the quantifiers which _wrap_ a parsed expression into a `Repetition` node. The quantifier itself can be of different _kinds_, and has `Quantifier` node type.

0 commit comments

Comments
 (0)