1
1
# API
2
2
3
+ ## Types
4
+
5
+ ### ` RegexSequence `
6
+
7
+ The sequence of regex elements forming a regular expression. For developer convenience it also accepts a single element instead of array.
8
+
9
+ ### ` RegexElement `
10
+
11
+ Fundamental building blocks of a regular expression, defined as either a regex construct or a string.
12
+
13
+ ### ` RegexConstruct `
14
+
15
+ The common type for all regex constructs like character classes, quantifiers, and anchors. You should not need to use this type directly, it is returned by all regex construct functions.
16
+
17
+ Note: the shape of the ` RegexConstruct ` is considered private, and may change in a breaking way without a major release. We will focus on maintaining the compatibility of regexes built with
18
+
19
+
3
20
## Builder
4
21
5
- ### ` buildRegExp() ` function
22
+ ### ` buildRegExp() `
6
23
7
24
``` ts
8
- function buildRegExp(sequence : RegexSequence ): RegExp ;
9
-
10
25
function buildRegExp(
11
- sequence : RegexSequence ,
12
- flags : {
13
- global? : boolean ;
14
- ignoreCase? : boolean ;
15
- multiline? : boolean ;
16
- hasIndices? : boolean ;
17
- sticky? : boolean ;
18
- },
26
+ sequence : RegexSequence ,
27
+ flags ? : {
28
+ global? : boolean ;
29
+ ignoreCase? : boolean ;
30
+ multiline? : boolean ;
31
+ hasIndices? : boolean ;
32
+ },
19
33
): RegExp ;
20
34
```
21
35
36
+ The ` buildRegExp ` is a top-level function responsible for build JavaScript-native ` RegExp ` object from passed regex sequence.
37
+
38
+ It optionally accepts a list of regex flags:
39
+
40
+ - ` global ` - find all matches in a string, instead of just the first one.
41
+ - ` ignoreCase ` - perform case-insensitive matching.
42
+ - ` multiline ` - treat the start and end of each line in a string as the beginning and end of the string.
43
+ - ` hasIndices ` - provide the start and end indices of each captured group in a match.
44
+
22
45
## Constructs
23
46
24
- ### ` capture() `
47
+ These functions and objects represent available regex constructs.
25
48
26
- Captures, also known as capturing groups, are used to extract and store parts of the matched string for later use.
49
+ ### ` capture() `
27
50
28
51
``` ts
29
52
function capture(
30
- sequence : RegexSequence
53
+ sequence : RegexSequence
31
54
): Capture
32
55
```
33
56
57
+ Regex syntax : ` (...) ` .
58
+
59
+ Captures , also known as capturing groups , are used to extract and store parts of the matched string for later use .
60
+
34
61
### ` choiceOf() `
35
62
36
63
` ` ` ts
37
64
function choiceOf(
38
- ...alternatives: RegexSequence[]
65
+ ...alternatives: RegexSequence[]
39
66
): ChoiceOf {
40
67
` ` `
41
68
42
- The ` choiceOf ` (alternation ) construct is used to match one out of several possible sequences . It functions similarly to a logical OR operator in programming . It can match simple string options as well as complex patterns .
69
+ Regex syntax : ` a|b|c ` .
70
+
71
+ The ` choiceOf ` (disjunction ) construct is used to match one out of several possible sequences . It functions similarly to a logical OR operator in programming . It can match simple string options as well as complex patterns .
43
72
44
73
Example : ` choiceOf("color", "colour") ` matches either ` color ` or ` colour ` pattern .
45
74
46
75
## Quantifiers
47
76
77
+ Quantifiers in regex define the number of occurrences to match for a pattern .
78
+
48
79
### ` zeroOrMore() `
49
80
50
81
` ` ` ts
51
82
function zeroOrMore(
52
- sequence: RegexSequence,
83
+ sequence: RegexSequence,
53
84
): ZeroOrMore
54
85
` ` `
55
86
87
+ Regex syntax : ` x* ` ;
88
+
89
+ The ` zeroOrMore ` quantifier matches zero or more occurrences of given pattern , allowing a flexible number of repetitions of that element .
90
+
56
91
### ` oneOrMore() `
57
92
58
93
` ` ` ts
59
94
function oneOrMore(
60
- sequence: RegexSequence,
95
+ sequence: RegexSequence,
61
96
): OneOrMore
62
97
` ` `
63
98
99
+ Regex syntax : ` x+ ` ;
100
+
101
+ The ` oneOrMore ` quantifier matches one or more occurrences of given pattern , allowing a flexible number of repetitions of that element .
102
+
64
103
### ` optionally() `
65
104
66
105
` ` ` ts
67
106
function optionally(
68
- sequence: RegexSequence,
107
+ sequence: RegexSequence,
69
108
): Optionally
70
109
` ` `
71
110
111
+ Regex syntax : ` x? ` ;
112
+
113
+ The ` optionally ` quantifier matches zero or one occurrence of given pattern , making it optional .
114
+
72
115
### ` repeat() `
73
116
74
117
` ` ` ts
75
118
function repeat(
76
- options: number | { min: number; max?: number } ,
77
- sequence: RegexSequence ,
119
+ sequence: RegexSequence ,
120
+ count: number | { min: number; max?: number } ,
78
121
): Repeat
79
122
` ` `
80
123
124
+ Regex syntax : ` x{n} ` , ` x{min,} ` , ` x{min, max} ` .
125
+
126
+ The ` repeat ` quantifier in regex matches either exactly ` count ` times or between ` min ` and ` max ` times . If only ` min ` is provided it matches at least ` min ` times .
127
+
81
128
## Character classes
82
129
83
- Character classes are a set of characters that match any one of the characters in the set .
130
+ Character classes are a set of characters that match any one of the characters in the set .
84
131
85
132
### Common character classess
86
133
@@ -91,80 +138,88 @@ const digit: CharacterClass;
91
138
const whitespace: CharacterClass;
92
139
` ` `
93
140
94
- * ` any ` matches any character except newline characters .
95
- * ` word ` matches any word character (alphanumeric & underscore ).
96
- * ` digit ` matches any digit .
97
- * ` whitespace ` matches any whitespace character (spaces , tabs , line breaks ).
141
+ - ` any ` matches any character except newline characters . Regex syntax : ` * ` .
142
+ - ` word ` matches any word character (alphanumeric & underscore ). Regex syntax : ` \w ` .
143
+ - ` digit ` matches any digit . Regex syntax : ` \d ` .
144
+ - ` whitespace ` matches any whitespace character (spaces , tabs , line breaks ). Regex syntax : ` \s ` .
98
145
99
146
### ` anyOf() `
100
147
101
148
` ` ` ts
102
149
function anyOf(
103
- characters: string,
150
+ characters: string,
104
151
): CharacterClass
105
152
` ` `
106
153
154
+ Regex syntax : ` [abc] ` .
155
+
107
156
The ` anyOf ` class matches any character present in the `character ` string .
108
157
109
158
Example : `anyOf ('aeiou ')` will match either `a `, `e `, `i ` `o ` or `u ` characters .
110
159
111
- ### `characterRange ()`
160
+ ### `charRange ()`
112
161
113
162
```ts
114
- function characterRange (
115
- start : string ,
116
- end : string ,
163
+ function charRange (
164
+ start : string ,
165
+ end : string ,
117
166
): CharacterClass
118
167
```
119
168
120
- The `characterRange ` class matches any character present in the range from `start ` to `end ` (inclusive ).
169
+ Regex syntax : `[a -z ]`.
170
+
171
+ The `charRange ` class matches any character present in the range from `start ` to `end ` (inclusive ).
121
172
122
173
Examples :
123
- * `characterRange ('a ', 'z ')` will match all lowercase characters from `a ` to `z `.
124
- * `characterRange ('A ', 'Z ')` will match all uppercase characters from `a ` to `z `.
125
- * `characterRange ('0', '9')` will match all digit characters from `0` to `9`.
126
174
127
- ### `characterClass ()`
175
+ - `charRange ('a ', 'z ')` will match all lowercase characters from `a ` to `z `.
176
+ - `charRange ('A ', 'Z ')` will match all uppercase characters from `A ` to `Z `.
177
+ - `charRange ('0', '9')` will match all digit characters from `0` to `9`.
178
+
179
+ ### `charClass ()`
128
180
129
181
```ts
130
- function characterClass (
131
- ...elements : CharacterClass [],
182
+ function charClass (
183
+ ...elements : CharacterClass [],
132
184
): CharacterClass
133
185
```
134
186
135
- The ` characterClass ` construct creates a new character class that includes all passed character classes .
187
+ Regex syntax : `[...]` .
136
188
137
- Example :
138
- * `characterClass (characterRange ('a ', 'f '), digit )` will match all lowercase hex digits (`0` to `9` and `a ` to `f `).
139
- * `characterClass (characterRange ('a ', 'z '), digit , anyOf ("._ -"))` will match any digit , lowercase latin lettet from `a ` to `z `, and either of `.`, `_ `, and `-` characters .
189
+ The `charClass ` construct creates a new character class that includes all passed character classes .
190
+
191
+ Examples :
192
+
193
+ - `charClass (charRange ('a ', 'f '), digit )` will match all lowercase hex digits (`0` to `9` and `a ` to `f `).
194
+ - `charClass (charRange ('a ', 'z '), digit , anyOf ("._ -"))` will match any digit , lowercase latin lettet from `a ` to `z `, and either of `.`, `_ `, and `-` characters .
140
195
141
196
### `inverted ()`
142
197
143
198
```ts
144
199
function inverted (
145
- element : CharacterClass ,
200
+ element : CharacterClass ,
146
201
): CharacterClass
147
202
```
148
203
204
+ Regex syntax : `[^...]`.
205
+
149
206
The `inverted ` construct creates a new character class that matches any character that is not present in the passed character class .
150
207
151
208
Examples :
152
- * `inverted (digit )` matches any character that is not a digit
153
- * `inverted (anyOf ('aeiou '))` matches any character that is not a lowercase vowel .
154
-
155
209
210
+ - `inverted (digit )` matches any character that is not a digit
211
+ - `inverted (anyOf ('aeiou '))` matches any character that is not a lowercase vowel .
156
212
157
213
## Anchors
158
214
159
215
Anchors are special characters or sequences that specify positions in the input string , rather than matching specific characters .
160
216
161
- ### Line start and end
217
+ ### Start and end of string
162
218
163
219
```ts
164
- const startOfString : Anchor ; // Regex: ^
165
- const endOfString : Anchor ; // Regex: $
220
+ const startOfString : Anchor ;
221
+ const endOfString : Anchor ;
166
222
```
167
223
168
- The `startOfString ` (regex : `^`) matches the start of a string (or line , if multiline mode is enabled ).
169
-
170
- The `endOfString ` (regex : `$ `) matches the end of a string (or line , if multiline mode is enabled ).
224
+ - `startOfString ` anchor matches the start of a string (or line , if multiline mode is enabled ). Regex syntax : `^`.
225
+ - `endOfString ` anchor matches the end of a string (or line , if multiline mode is enabled ). Regex syntax : `$ `.
0 commit comments