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
> 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
+
714
750
##### Non-capturing group
715
751
716
752
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`:
757
793
758
794
##### Backreferences
759
795
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.
761
797
762
798
Matches `abab` string:
763
799
@@ -792,12 +828,54 @@ A node:
792
828
},
793
829
{
794
830
type:'Backreference',
831
+
kind:'number',
832
+
number:1,
795
833
reference:1,
796
834
}
797
835
]
798
836
}
799
837
```
800
838
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
+
801
879
#### Quantifiers
802
880
803
881
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