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
-`(?<name>...)` for named capturing groups (`name` option)
21
+
22
+
Captures, also known as capturing groups, extract and store parts of the matched string for later use.
23
+
24
+
Capture results are available using array-like [`match()` result object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#using_match).
25
+
26
+
#### Named groups
27
+
28
+
When using `name` options, the group becomes a [named capturing group](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Named_capturing_group) allowing to refer to it using name instead of index.
29
+
30
+
Named capture results are available using [`groups`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#using_named_capturing_groups) property on `match()` result.
31
+
32
+
:::note
33
+
34
+
TS Regex Builder does not have a construct for non-capturing groups. Such groups are implicitly added when required. E.g., `zeroOrMore("abc")` is encoded as `(?:abc)+`.
35
+
36
+
:::
37
+
38
+
### `ref()`
39
+
40
+
```ts
41
+
function ref(name:string):Reference;
42
+
```
43
+
44
+
Regex syntax: `\k<...>`.
45
+
46
+
Creates a reference, also known as a backreference, which allows matching again the exact text that a capturing group previously matched. The reference must use the same name as some capturing group earlier in the expression to form a valid regex.
47
+
48
+
Usage with `capture()`:
49
+
50
+
```ts
51
+
const regex =buildRegExp([
52
+
// Create a named capture using name from `someKey`.
53
+
capture(..., { name: 'someKey' }),
54
+
// ... some other elements ...
55
+
56
+
// Match the same text as matched by `capture` with the same name.
57
+
ref('someKey'),
58
+
])
59
+
```
60
+
61
+
:::note
62
+
63
+
TS Regex Builder doesn't support using ordinal backreferences (`\1`, `\2`, etc) because in complex regex patterns, these references are difficult to accurately use.
Copy file name to clipboardexpand all lines: website/docs/api/constructs.md
+1-21
Original file line number
Diff line number
Diff line change
@@ -19,30 +19,10 @@ The `choiceOf` (disjunction) construct matches one out of several possible seque
19
19
20
20
Example: `choiceOf("color", "colour")` matches either `color` or `colour` pattern.
21
21
22
-
### `capture()`
23
-
24
-
```ts
25
-
function capture(
26
-
sequence:RegexSequence,
27
-
):Capture;
28
-
```
29
-
30
-
Regex syntax: `(...)`.
31
-
32
-
Captures, also known as capturing groups, extract and store parts of the matched string for later use.
33
-
34
-
:::note
35
-
36
-
TS Regex Builder does not have a construct for non-capturing groups. Such groups are implicitly added when required. E.g., `zeroOrMore("abc")` is encoded as `(?:abc)+`.
37
-
38
-
:::
39
-
40
22
### `regex()`
41
23
42
24
```ts
43
-
function regex(
44
-
sequence:RegexSequence,
45
-
):Regex;
25
+
function regex(sequence:RegexSequence):Regex;
46
26
```
47
27
48
28
Regex syntax: the pattern remains unchanged when wrapped by this construct.
Copy file name to clipboardexpand all lines: website/docs/api/types.md
+2-7
Original file line number
Diff line number
Diff line change
@@ -6,20 +6,15 @@ title: Types
6
6
### `RegexSequence`
7
7
8
8
```ts
9
-
typeRegexSequence=
10
-
|RegexElement[]
11
-
|RegexElement;
9
+
typeRegexSequence=RegexElement[] |RegexElement;
12
10
```
13
11
14
12
The sequence of regex elements forming a regular expression. For developer convenience, it also accepts a single element instead of an array.
15
13
16
14
### `RegexElement`
17
15
18
16
```ts
19
-
typeRegexElement=
20
-
|RegexConstruct
21
-
|RegExp
22
-
|string;
17
+
typeRegexElement=RegexConstruct|RegExp|string;
23
18
```
24
19
25
20
Regex elements are fundamental building blocks of a regular expression. These can be either further regex constructs, regular strings to be matched literally or `RegExp` literals (`/.../`) for including simple regexes as part of a larger structure.
0 commit comments