rregex 1.7.1 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +154 -47
- package/lib/browser.d.ts +790 -278
- package/lib/browser.js +649 -179
- package/lib/commonjs.d.ts +779 -271
- package/lib/commonjs.js +659 -182
- package/lib/module.d.ts +779 -271
- package/lib/module.js +656 -179
- package/lib/rregex.wasm +0 -0
- package/lib/rregex.wasm.d.ts +15 -11
- package/lib/types.d.ts +790 -278
- package/package.json +23 -2
package/README.md
CHANGED
|
@@ -4,22 +4,26 @@ A WebAssembly build of [Rust Regex](https://docs.rs/regex/latest/regex/) for Jav
|
|
|
4
4
|
|
|
5
5
|
> Note: this project is not intended to be used in production jet
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
7
|
+
- [Why Rust Regex](#why-rust-regex)
|
|
8
|
+
- [Install](#install)
|
|
9
|
+
- [API](#api)
|
|
10
|
+
- [`isMatch(text: string): boolean`](#ismatchtext-string-boolean)
|
|
11
|
+
- [`isMatchAt(text: string, limit: number): boolean`](#ismatchattext-string-limit-number-boolean)
|
|
12
|
+
- [`find(text: string): Match | undefined;`](#findtext-string-match--undefined)
|
|
13
|
+
- [`findAt(text: string): Match | undefined;`](#findattext-string-match--undefined)
|
|
14
|
+
- [`findAll(text: string): Match[];`](#findalltext-string-match)
|
|
15
|
+
- [`capturesLength(): number;`](#captureslength-number) (since 1.8)
|
|
16
|
+
- [`captureNames(): string[];`](#capturenames-string) (since 1.8)
|
|
17
|
+
- [`captures(text: string): Captures | undefined;`](#capturestext-string-captures--undefined) (since 1.8)
|
|
18
|
+
- [`capturesAll(text: string): Captures[];`](#capturesalltext-string-captures) (since 1.8)
|
|
19
|
+
- [`replace(text: string, rep: string): string;`](#replacetext-string-rep-string-string)
|
|
20
|
+
- [`replaceAll(text: string, rep: string): string;`](#replacealltext-string-rep-string-string)
|
|
21
|
+
- [`replacen(text: string, limit: number, rep: string): string;`](#replacentext-string-limit-number-rep-string-string)
|
|
22
|
+
- [`split(text: string): string[];`](#splittext-string-string)
|
|
23
|
+
- [`splitn(text: string, limit: number): string[];`](#splitntext-string-limit-number-string)
|
|
24
|
+
- [`shortestMatch(text: string): number | undefined;`](#shortestmatchtext-string-number--undefined)
|
|
25
|
+
- [`shortestMatchAt(text: string, limit: number): number | undefined;`](#shortestmatchattext-string-limit-number-number--undefined)
|
|
26
|
+
- [Known Issues](#known-issues)
|
|
23
27
|
|
|
24
28
|
## Why Rust Regex
|
|
25
29
|
|
|
@@ -47,7 +51,7 @@ assert.equal(re.isMatch("2014-01-01"), true);
|
|
|
47
51
|
|
|
48
52
|
### `isMatch(text: string): boolean;`
|
|
49
53
|
|
|
50
|
-
Returns true if and only if there is a match for the regex in the string given.
|
|
54
|
+
Returns true if and only if there is a match for the regex in the string given.
|
|
51
55
|
|
|
52
56
|
```typescript
|
|
53
57
|
const text = "I categorically deny having triskaidekaphobia.";
|
|
@@ -55,9 +59,13 @@ const re = new RRegex("\\b\\w{13}\\b");
|
|
|
55
59
|
expect(re.isMatch(text)).toEqual(true);
|
|
56
60
|
```
|
|
57
61
|
|
|
62
|
+
[See Documentation for `is_match`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.is_match)
|
|
63
|
+
|
|
58
64
|
### `isMatchAt(text: string, limit: number): boolean;`
|
|
59
65
|
|
|
60
|
-
Returns the same as is_match, but starts the search at the given offset.
|
|
66
|
+
Returns the same as is_match, but starts the search at the given offset.
|
|
67
|
+
|
|
68
|
+
[See Documentation for `is_match_at`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.is_match_at)
|
|
61
69
|
|
|
62
70
|
```typescript
|
|
63
71
|
const text = "I categorically deny having triskaidekaphobia.";
|
|
@@ -68,7 +76,7 @@ expect(re.isMatchAt(text, 5)).toBe(false);
|
|
|
68
76
|
|
|
69
77
|
### `find(text: string): Match | undefined;`
|
|
70
78
|
|
|
71
|
-
Returns the start and end byte range of the leftmost-first match in `text`. If no match exists, then `undefined` is returned.
|
|
79
|
+
Returns the start and end byte range of the leftmost-first match in `text`. If no match exists, then `undefined` is returned.
|
|
72
80
|
|
|
73
81
|
```typescript
|
|
74
82
|
const text = "I categorically deny having triskaidekaphobia.";
|
|
@@ -80,9 +88,11 @@ expect(re.find(text)).toEqual({
|
|
|
80
88
|
});
|
|
81
89
|
```
|
|
82
90
|
|
|
91
|
+
[See Documentation for `find`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.find)
|
|
92
|
+
|
|
83
93
|
### `findAt(text: string): Match | undefined;`
|
|
84
94
|
|
|
85
|
-
Returns the same as find, but starts the search at the given offset.
|
|
95
|
+
Returns the same as find, but starts the search at the given offset.
|
|
86
96
|
|
|
87
97
|
```typescript
|
|
88
98
|
const text = "I categorically deny having triskaidekaphobia.";
|
|
@@ -96,9 +106,11 @@ expect(re.findAt(text, 1)).toEqual({
|
|
|
96
106
|
expect(re.findAt(text, 5)).toEqual(undefined);
|
|
97
107
|
```
|
|
98
108
|
|
|
109
|
+
([See Documentation for `find_at`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.find_at))
|
|
110
|
+
|
|
99
111
|
### `findAll(text: string): Match[];`
|
|
100
112
|
|
|
101
|
-
Returns an array for each successive non-overlapping match in text, returning the start and end byte indices with respect to text
|
|
113
|
+
Returns an array for each successive non-overlapping match in text, returning the start and end byte indices with respect to text
|
|
102
114
|
|
|
103
115
|
```typescript
|
|
104
116
|
const text = 'Retroactively relinquishing remunerations is reprehensible.'
|
|
@@ -128,11 +140,94 @@ Returns an array for each successive non-overlapping match in text, returning th
|
|
|
128
140
|
})
|
|
129
141
|
```
|
|
130
142
|
|
|
143
|
+
[See Documentation for `find_iter`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.find_iter)
|
|
144
|
+
|
|
145
|
+
### `captureNames(): string[];`
|
|
146
|
+
|
|
147
|
+
> Note: available since 1.8
|
|
148
|
+
|
|
149
|
+
Returns a list of the capture names in this regex.
|
|
150
|
+
|
|
151
|
+
[See Documentation for `capture_names`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.capture_names)
|
|
152
|
+
|
|
153
|
+
### `captures(text: string): Captures | undefined;`
|
|
154
|
+
|
|
155
|
+
> Note: available since 1.8
|
|
156
|
+
|
|
157
|
+
Returns the capture groups corresponding to the leftmost-first
|
|
158
|
+
match in `text`. Capture group `0` always corresponds to the entire
|
|
159
|
+
match. If no match is found, then `undefined` is returned.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
const re = new Regex("'([^']+)'\\s+\\((\\d{4})\)")
|
|
163
|
+
const text = "Not my favorite movie: 'Citizen Kane' (1941)."
|
|
164
|
+
const caps = re.captures(text)
|
|
165
|
+
expect(caps.get[1].value).toBe("Citizen Kane")
|
|
166
|
+
expect(caps.get[2].value).toBe("1941")
|
|
167
|
+
expect(caps.get[0].value).toBe("'Citizen Kane' (1941)")
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### `capturesAll(text: string): Captures[];`
|
|
171
|
+
|
|
172
|
+
> Note: available since 1.8
|
|
173
|
+
|
|
174
|
+
Returns a list with all the non-overlapping capture groups matched
|
|
175
|
+
in `text`. This is operationally the same as `findAll`, except it
|
|
176
|
+
returns information about capturing group matches.
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
const re = new Regex("'(?P<title>[^']+)'\\s+\\((?P<year>\\d{4})\\)")
|
|
180
|
+
const text = "'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931)."
|
|
181
|
+
for caps of re.captures_iter(text) {
|
|
182
|
+
console.log(
|
|
183
|
+
"Movie:", caps.name["title"].value, ","
|
|
184
|
+
"Released:", caps.name["year"].value
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
// Output:
|
|
188
|
+
// Movie: Citizen Kane, Released: 1941
|
|
189
|
+
// Movie: The Wizard of Oz, Released: 1939
|
|
190
|
+
// Movie: M, Released: 1931
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
[See the documentation for `captures_iter`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.captures_iter))
|
|
194
|
+
|
|
195
|
+
### `capturesLength(): number;`
|
|
196
|
+
|
|
197
|
+
> Note: available since 1.8
|
|
198
|
+
|
|
199
|
+
Returns the number of captures.
|
|
200
|
+
|
|
201
|
+
This includes all named and unnamed groups, including the implicit
|
|
202
|
+
unnamed group that is always present and corresponds to the entire match.
|
|
203
|
+
|
|
204
|
+
Since the implicit unnamed group is always included in this length, the
|
|
205
|
+
length returned is guaranteed to be greater than zero.
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
const re1 = new RRegex("(?P<y>\\d{4})-(?P<m>\\d{2})-(?P<d>\\d{2})");
|
|
209
|
+
expect(re1.capturesLength()).toEqual(4);
|
|
210
|
+
|
|
211
|
+
const re2 = new RRegex("foo");
|
|
212
|
+
expect(re2.capturesLength()).toEqual(1);
|
|
213
|
+
|
|
214
|
+
const re3 = new RRegex("(foo)");
|
|
215
|
+
expect(re3.capturesLength()).toEqual(2);
|
|
216
|
+
|
|
217
|
+
const re4 = new RRegex("(?<a>.(?<b>.))(.)(?:.)(?<c>.)");
|
|
218
|
+
expect(re4.capturesLength()).toEqual(5);
|
|
219
|
+
|
|
220
|
+
const re5 = new RRegex("[a&&b]");
|
|
221
|
+
expect(re5.capturesLength()).toEqual(1);
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
[See the documentation for `captures_len`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.captures_len)
|
|
225
|
+
|
|
131
226
|
### `replace(text: string, rep: string): string;`
|
|
132
227
|
|
|
133
228
|
Replaces the leftmost-first match with the replacement provided.
|
|
134
229
|
|
|
135
|
-
If no match is found, then a copy of the string is returned unchanged.
|
|
230
|
+
If no match is found, then a copy of the string is returned unchanged.
|
|
136
231
|
|
|
137
232
|
Note that this function is polymorphic with respect to the replacement. In typical usage, this can just be a normal string:
|
|
138
233
|
|
|
@@ -161,19 +256,23 @@ expect(result).toEqual("deep_fried");
|
|
|
161
256
|
|
|
162
257
|
Without the curly braces, the capture group name `first_` would be used, and since it doesn’t exist, it would be replaced with the empty string.
|
|
163
258
|
|
|
259
|
+
[See Documentation for `replace`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.replace)
|
|
260
|
+
|
|
164
261
|
### `replaceAll(text: string, rep: string): string;`
|
|
165
262
|
|
|
166
263
|
Replaces all non-overlapping matches in `text` with the replacement provided. This is the same as calling `replacen` with `limit` set to `0`.
|
|
167
264
|
|
|
168
|
-
See the documentation for `
|
|
265
|
+
[See the documentation for `replace_all`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.replace_all)
|
|
169
266
|
|
|
170
267
|
### `replacen(text: string, limit: number, rep: string): string;`
|
|
171
268
|
|
|
172
|
-
Replaces at most `limit` non-overlapping matches in `text` with the replacement provided. If `limit` is 0, then all non-overlapping matches are replaced.
|
|
269
|
+
Replaces at most `limit` non-overlapping matches in `text` with the replacement provided. If `limit` is 0, then all non-overlapping matches are replaced.
|
|
270
|
+
|
|
271
|
+
[See Documentation for `replacen`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.replacen)
|
|
173
272
|
|
|
174
273
|
### `split(text: string): string[];`
|
|
175
274
|
|
|
176
|
-
Returns an iterator of substrings of `text` delimited by a match of the regular expression. Namely, each element of the iterator corresponds to text that isn’t matched by the regular expression.
|
|
275
|
+
Returns an iterator of substrings of `text` delimited by a match of the regular expression. Namely, each element of the iterator corresponds to text that isn’t matched by the regular expression.
|
|
177
276
|
|
|
178
277
|
```typescript
|
|
179
278
|
const re = new RRegex("[ \\t]+");
|
|
@@ -181,9 +280,11 @@ const fields = re.split("a b \t c\td e");
|
|
|
181
280
|
expect(fields).toEqual(["a", "b", "c", "d", "e"]);
|
|
182
281
|
```
|
|
183
282
|
|
|
283
|
+
[See Documentation for `split`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.split)
|
|
284
|
+
|
|
184
285
|
### `splitn(text: string, limit: number): string[];`
|
|
185
286
|
|
|
186
|
-
Returns an iterator of at most `limit` substrings of `text` delimited by a match of the regular expression. (A limit of 0 will return no substrings.) Namely, each element of the iterator corresponds to text that isn’t matched by the regular expression. The remainder of the string that is not split will be the last element in the iterator.
|
|
287
|
+
Returns an iterator of at most `limit` substrings of `text` delimited by a match of the regular expression. (A limit of 0 will return no substrings.) Namely, each element of the iterator corresponds to text that isn’t matched by the regular expression. The remainder of the string that is not split will be the last element in the iterator.
|
|
187
288
|
|
|
188
289
|
```typescript
|
|
189
290
|
const re = new RRegex("\\W+");
|
|
@@ -191,41 +292,47 @@ const fields = re.splitn("Hey! How are you?", 3);
|
|
|
191
292
|
expect(fields).toEqual(["Hey", "How", "are you?"]);
|
|
192
293
|
```
|
|
193
294
|
|
|
295
|
+
[See Documentation for `splitn`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.splitn)
|
|
296
|
+
|
|
194
297
|
### `shortestMatch(text: string): number | undefined;`
|
|
195
298
|
|
|
196
299
|
Returns the end location of a match in the text given.
|
|
197
300
|
|
|
198
|
-
This method may have the same performance characteristics as `is_match`, except it provides an end location for a match. In particular, the location returned may be shorter than the proper end of the leftmost-first match.
|
|
301
|
+
This method may have the same performance characteristics as `is_match`, except it provides an end location for a match. In particular, the location returned may be shorter than the proper end of the leftmost-first match.
|
|
199
302
|
|
|
200
303
|
```typescript
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
304
|
+
const text = "aaaaa";
|
|
305
|
+
const pos = new RRegex("a+");
|
|
306
|
+
expect(pos.shortestMatch(text)).toBe(1);
|
|
204
307
|
```
|
|
205
308
|
|
|
309
|
+
[See Documentation for `shortest_match`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.shortest_match)
|
|
310
|
+
|
|
206
311
|
### `shortestMatchAt(text: string, limit: number): number | undefined;`
|
|
207
312
|
|
|
208
|
-
Returns the same as shortest_match, but starts the search at the given offset.
|
|
313
|
+
Returns the same as shortest_match, but starts the search at the given offset.
|
|
314
|
+
|
|
315
|
+
[See Documentation for `shortest_match_at`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.shortest_match_at)
|
|
209
316
|
|
|
210
317
|
## Known Issues
|
|
211
318
|
|
|
212
319
|
If you call `splitn(text, limit)` and the expected result length is equal to `limit - 1` the result will include an extra item `""`, this behavior does not happen if `limit` es greater. **`fixed at >=1.3`**
|
|
213
320
|
|
|
214
321
|
```ts
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
322
|
+
const regex = new RRegex(",");
|
|
323
|
+
expect(regex.splitn("a,b,c", 0)).toEqual([]);
|
|
324
|
+
expect(regex.splitn("a,b,c", 1)).toEqual(["a,b,c"]);
|
|
325
|
+
expect(regex.splitn("a,b,c", 2)).toEqual(["a", "b,c"]);
|
|
326
|
+
expect(regex.splitn("a,b,c", 3)).toEqual(["a", "b", "c"]);
|
|
327
|
+
|
|
328
|
+
// This result includes an unexpected extra item
|
|
329
|
+
expect(regex.splitn("a,b,c", 4)).toEqual(["a", "b", "c", ""]);
|
|
330
|
+
expect(regex.splitn("a,b,c", 5)).toEqual(["a", "b", "c"]);
|
|
331
|
+
|
|
332
|
+
expect(regex.splitn("abc", 0)).toEqual([]);
|
|
333
|
+
expect(regex.splitn("abc", 1)).toEqual(["abc"]);
|
|
334
|
+
|
|
335
|
+
// This result includes an unexpected extra item
|
|
336
|
+
expect(regex.splitn("abc", 2)).toEqual(["abc", ""]);
|
|
337
|
+
expect(regex.splitn("abc", 3)).toEqual(["abc"]);
|
|
231
338
|
```
|