Skip to content

Commit 0a411bc

Browse files
committed
docs: add docs
1 parent 9b39e59 commit 0a411bc

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

website/docs/api/constructs.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ These functions and objects represent available regex constructs.
99

1010
```ts
1111
function choiceOf(
12-
...alternatives: RegexSequence[]
12+
...alternatives: RegexSequence[],
1313
): ChoiceOf {
1414
```
1515
@@ -22,7 +22,9 @@ Example: `choiceOf("color", "colour")` matches either `color` or `colour` patter
2222
### `capture()`
2323
2424
```ts
25-
function capture(sequence: RegexSequence): Capture;
25+
function capture(
26+
sequence: RegexSequence,
27+
): Capture;
2628
```
2729
2830
Regex syntax: `(...)`.
@@ -35,3 +37,34 @@ TS Regex Builder does not have a construct for non-capturing groups. Such groups
3537
3638
:::
3739
40+
### `regex()`
41+
42+
```ts
43+
function regex(
44+
sequence: RegexSequence,
45+
): Regex;
46+
```
47+
48+
Regex syntax: the pattern remains unchanged when wrapped by this construct.
49+
50+
This construct is a no-op operator that groups array of `RegexElements` into a single element for composition purposes. This is particularly useful for defining smaller sequence patterns as separate variables.
51+
52+
Without `regex()`:
53+
54+
```ts
55+
const exponent = [anyOf('eE'), optional(anyOf('+-')), oneOrMore(digit)];
56+
const numberWithExponent = buildRegExp([
57+
oneOrMore(digit),
58+
...exponent, // Need to spread "exponent" as it's an array.
59+
]);
60+
```
61+
62+
With `regex()`:
63+
64+
```ts
65+
const exponent = regex([anyOf('eE'), optional(anyOf('+-')), oneOrMore(digit)]);
66+
const numberWithExponent = buildRegExp([
67+
oneOrMore(digit),
68+
exponent, // Easily compose "exponent" sequence as a single element.
69+
]);
70+
```

0 commit comments

Comments
 (0)