re2js 2.8.4 → 2.8.5
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 +31 -0
- package/build/index.cjs +30 -3
- package/build/index.d.cts +14 -4
- package/build/index.d.ts +14 -4
- package/build/index.js +30 -3
- package/build/index.umd.js +30 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -228,6 +228,37 @@ matchString.group() // 'e'
|
|
|
228
228
|
matchString.find(7) // false
|
|
229
229
|
```
|
|
230
230
|
|
|
231
|
+
### Executing a Search (`exec`)
|
|
232
|
+
|
|
233
|
+
If you want a native, 1:1 drop-in replacement for JavaScript's `RegExp.prototype.exec()`, you can use the `.exec()` method.
|
|
234
|
+
|
|
235
|
+
Instead of dealing with a stateful `Matcher` object, `exec()` performs a single search and returns a standard `RegExpExecArray`-shaped result, or `null` if no match is found. It includes the `.index`, `.input`, and `.groups` properties, and accurately maps unmatched optional capture groups to `undefined` (just like native JavaScript).
|
|
236
|
+
|
|
237
|
+
```js
|
|
238
|
+
import { RE2JS } from 're2js'
|
|
239
|
+
|
|
240
|
+
const re = RE2JS.compile('(?P<first>\\w+) (?:(?P<middle>\\w+) )?(?P<last>\\w+)');
|
|
241
|
+
const result = re.exec('John Doe');
|
|
242
|
+
|
|
243
|
+
if (result !== null) {
|
|
244
|
+
console.log(result[0]); // "John Doe" (Full match)
|
|
245
|
+
console.log(result[1]); // "John" (Group 1)
|
|
246
|
+
console.log(result[2]); // undefined (Group 2 didn't match)
|
|
247
|
+
console.log(result[3]); // "Doe" (Group 3)
|
|
248
|
+
|
|
249
|
+
console.log(result.index); // 0
|
|
250
|
+
console.log(result.input); // "John Doe"
|
|
251
|
+
|
|
252
|
+
// Named groups dictionary
|
|
253
|
+
console.log(result.groups.first); // "John"
|
|
254
|
+
console.log(result.groups.middle); // undefined
|
|
255
|
+
console.log(result.groups.last); // "Doe"
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
***Performance Note:** If you are running `exec()` inside a `while` loop to manually extract multiple matches globally, it is highly recommended to use `.matchAll()` instead, as it is cleaner, strictly stateless, and avoids infinite loop pitfalls.*
|
|
261
|
+
|
|
231
262
|
### Iterating Over Matches (`matchAll`)
|
|
232
263
|
|
|
233
264
|
For a more modern, JavaScript-native developer experience, RE2JS provides a `.matchAll()` method. This returns an ES6 `IterableIterator`, allowing you to safely and cleanly iterate over matches using `for...of` loops or the array spread operator `[...]`.
|
package/build/index.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* re2js
|
|
3
3
|
* RE2JS is the JavaScript port of RE2, a regular expression engine that provides linear time matching
|
|
4
4
|
*
|
|
5
|
-
* @version v2.8.
|
|
5
|
+
* @version v2.8.5
|
|
6
6
|
* @author Oleksii Vasyliev
|
|
7
7
|
* @homepage https://github.com/le0pard/re2js#readme
|
|
8
8
|
* @repository github:le0pard/re2js
|
|
@@ -1246,7 +1246,7 @@ var Matcher = class Matcher {
|
|
|
1246
1246
|
}
|
|
1247
1247
|
/**
|
|
1248
1248
|
* Resets the {@code Matcher} and changes the input.
|
|
1249
|
-
* @param {MatcherInputBase} input
|
|
1249
|
+
* @param {string|number[]|Uint8Array|MatcherInputBase} input
|
|
1250
1250
|
* @returns {Matcher} the {@code Matcher} itself, for chained method calls
|
|
1251
1251
|
*/
|
|
1252
1252
|
resetMatcherInput(input) {
|
|
@@ -6366,6 +6366,33 @@ var RE2JS = class RE2JS {
|
|
|
6366
6366
|
return this.re2Input.executeEngine(machineInput, 0, RE2Flags.ANCHOR_BOTH, 0) !== null;
|
|
6367
6367
|
}
|
|
6368
6368
|
/**
|
|
6369
|
+
* Executes a search for a match in a specified string.
|
|
6370
|
+
* Returns a result array, or null if no match is found.
|
|
6371
|
+
* The returned array perfectly mirrors standard JavaScript `RegExpExecArray`,
|
|
6372
|
+
* including `.index`, `.input`, and `.groups` properties.
|
|
6373
|
+
*
|
|
6374
|
+
* @param {string|number[]|Uint8Array} input the input string or byte array
|
|
6375
|
+
* @returns {Array|null} the match array with index, input, and groups properties, or null
|
|
6376
|
+
*/
|
|
6377
|
+
exec(input) {
|
|
6378
|
+
const m = this.matcher(input);
|
|
6379
|
+
if (!m.find()) return null;
|
|
6380
|
+
const result = [m.group(0)];
|
|
6381
|
+
for (let i = 1; i <= m.groupCount(); i++) {
|
|
6382
|
+
const val = m.group(i);
|
|
6383
|
+
result.push(val === null ? void 0 : val);
|
|
6384
|
+
}
|
|
6385
|
+
result.index = m.start(0);
|
|
6386
|
+
result.input = input;
|
|
6387
|
+
const namedGroups = this.namedGroups();
|
|
6388
|
+
if (Object.keys(namedGroups).length > 0) {
|
|
6389
|
+
const parsedGroups = m.getNamedGroups();
|
|
6390
|
+
for (const key in parsedGroups) if (parsedGroups[key] === null) parsedGroups[key] = void 0;
|
|
6391
|
+
result.groups = parsedGroups;
|
|
6392
|
+
} else result.groups = void 0;
|
|
6393
|
+
return result;
|
|
6394
|
+
}
|
|
6395
|
+
/**
|
|
6369
6396
|
* Splits input around instances of the regular expression. It returns an array giving the strings
|
|
6370
6397
|
* that occur before, between, and after instances of the regular expression.
|
|
6371
6398
|
*
|
|
@@ -6417,7 +6444,7 @@ var RE2JS = class RE2JS {
|
|
|
6417
6444
|
* including capturing groups.
|
|
6418
6445
|
*
|
|
6419
6446
|
* @param {string|number[]|Uint8Array} input the input string or byte array
|
|
6420
|
-
* @returns {IterableIterator<
|
|
6447
|
+
* @returns {IterableIterator<RegExpMatchArray>}
|
|
6421
6448
|
*/
|
|
6422
6449
|
*matchAll(input) {
|
|
6423
6450
|
const m = this.matcher(input);
|
package/build/index.d.cts
CHANGED
|
@@ -221,10 +221,10 @@ export class Matcher {
|
|
|
221
221
|
anchorFlag: number;
|
|
222
222
|
/**
|
|
223
223
|
* Resets the {@code Matcher} and changes the input.
|
|
224
|
-
* @param {MatcherInputBase} input
|
|
224
|
+
* @param {string|number[]|Uint8Array|MatcherInputBase} input
|
|
225
225
|
* @returns {Matcher} the {@code Matcher} itself, for chained method calls
|
|
226
226
|
*/
|
|
227
|
-
resetMatcherInput(input: MatcherInputBase): Matcher;
|
|
227
|
+
resetMatcherInput(input: string | number[] | Uint8Array | MatcherInputBase): Matcher;
|
|
228
228
|
matcherInput: MatcherInputBase;
|
|
229
229
|
/**
|
|
230
230
|
* Returns the start of the named group of the most recent match, or -1 if the group was not
|
|
@@ -873,6 +873,16 @@ export class RE2JS {
|
|
|
873
873
|
* @returns {boolean} `true` if the exact input string fully matches the pattern, `false` otherwise.
|
|
874
874
|
*/
|
|
875
875
|
testExact(input: string | number[] | Uint8Array): boolean;
|
|
876
|
+
/**
|
|
877
|
+
* Executes a search for a match in a specified string.
|
|
878
|
+
* Returns a result array, or null if no match is found.
|
|
879
|
+
* The returned array perfectly mirrors standard JavaScript `RegExpExecArray`,
|
|
880
|
+
* including `.index`, `.input`, and `.groups` properties.
|
|
881
|
+
*
|
|
882
|
+
* @param {string|number[]|Uint8Array} input the input string or byte array
|
|
883
|
+
* @returns {Array|null} the match array with index, input, and groups properties, or null
|
|
884
|
+
*/
|
|
885
|
+
exec(input: string | number[] | Uint8Array): any[] | null;
|
|
876
886
|
/**
|
|
877
887
|
* Splits input around instances of the regular expression. It returns an array giving the strings
|
|
878
888
|
* that occur before, between, and after instances of the regular expression.
|
|
@@ -892,9 +902,9 @@ export class RE2JS {
|
|
|
892
902
|
* including capturing groups.
|
|
893
903
|
*
|
|
894
904
|
* @param {string|number[]|Uint8Array} input the input string or byte array
|
|
895
|
-
* @returns {IterableIterator<
|
|
905
|
+
* @returns {IterableIterator<RegExpMatchArray>}
|
|
896
906
|
*/
|
|
897
|
-
matchAll(input: string | number[] | Uint8Array): IterableIterator<
|
|
907
|
+
matchAll(input: string | number[] | Uint8Array): IterableIterator<RegExpMatchArray>;
|
|
898
908
|
/**
|
|
899
909
|
*
|
|
900
910
|
* @returns {string}
|
package/build/index.d.ts
CHANGED
|
@@ -221,10 +221,10 @@ export class Matcher {
|
|
|
221
221
|
anchorFlag: number;
|
|
222
222
|
/**
|
|
223
223
|
* Resets the {@code Matcher} and changes the input.
|
|
224
|
-
* @param {MatcherInputBase} input
|
|
224
|
+
* @param {string|number[]|Uint8Array|MatcherInputBase} input
|
|
225
225
|
* @returns {Matcher} the {@code Matcher} itself, for chained method calls
|
|
226
226
|
*/
|
|
227
|
-
resetMatcherInput(input: MatcherInputBase): Matcher;
|
|
227
|
+
resetMatcherInput(input: string | number[] | Uint8Array | MatcherInputBase): Matcher;
|
|
228
228
|
matcherInput: MatcherInputBase;
|
|
229
229
|
/**
|
|
230
230
|
* Returns the start of the named group of the most recent match, or -1 if the group was not
|
|
@@ -873,6 +873,16 @@ export class RE2JS {
|
|
|
873
873
|
* @returns {boolean} `true` if the exact input string fully matches the pattern, `false` otherwise.
|
|
874
874
|
*/
|
|
875
875
|
testExact(input: string | number[] | Uint8Array): boolean;
|
|
876
|
+
/**
|
|
877
|
+
* Executes a search for a match in a specified string.
|
|
878
|
+
* Returns a result array, or null if no match is found.
|
|
879
|
+
* The returned array perfectly mirrors standard JavaScript `RegExpExecArray`,
|
|
880
|
+
* including `.index`, `.input`, and `.groups` properties.
|
|
881
|
+
*
|
|
882
|
+
* @param {string|number[]|Uint8Array} input the input string or byte array
|
|
883
|
+
* @returns {Array|null} the match array with index, input, and groups properties, or null
|
|
884
|
+
*/
|
|
885
|
+
exec(input: string | number[] | Uint8Array): any[] | null;
|
|
876
886
|
/**
|
|
877
887
|
* Splits input around instances of the regular expression. It returns an array giving the strings
|
|
878
888
|
* that occur before, between, and after instances of the regular expression.
|
|
@@ -892,9 +902,9 @@ export class RE2JS {
|
|
|
892
902
|
* including capturing groups.
|
|
893
903
|
*
|
|
894
904
|
* @param {string|number[]|Uint8Array} input the input string or byte array
|
|
895
|
-
* @returns {IterableIterator<
|
|
905
|
+
* @returns {IterableIterator<RegExpMatchArray>}
|
|
896
906
|
*/
|
|
897
|
-
matchAll(input: string | number[] | Uint8Array): IterableIterator<
|
|
907
|
+
matchAll(input: string | number[] | Uint8Array): IterableIterator<RegExpMatchArray>;
|
|
898
908
|
/**
|
|
899
909
|
*
|
|
900
910
|
* @returns {string}
|
package/build/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* re2js
|
|
3
3
|
* RE2JS is the JavaScript port of RE2, a regular expression engine that provides linear time matching
|
|
4
4
|
*
|
|
5
|
-
* @version v2.8.
|
|
5
|
+
* @version v2.8.5
|
|
6
6
|
* @author Oleksii Vasyliev
|
|
7
7
|
* @homepage https://github.com/le0pard/re2js#readme
|
|
8
8
|
* @repository github:le0pard/re2js
|
|
@@ -1245,7 +1245,7 @@ var Matcher = class Matcher {
|
|
|
1245
1245
|
}
|
|
1246
1246
|
/**
|
|
1247
1247
|
* Resets the {@code Matcher} and changes the input.
|
|
1248
|
-
* @param {MatcherInputBase} input
|
|
1248
|
+
* @param {string|number[]|Uint8Array|MatcherInputBase} input
|
|
1249
1249
|
* @returns {Matcher} the {@code Matcher} itself, for chained method calls
|
|
1250
1250
|
*/
|
|
1251
1251
|
resetMatcherInput(input) {
|
|
@@ -6365,6 +6365,33 @@ var RE2JS = class RE2JS {
|
|
|
6365
6365
|
return this.re2Input.executeEngine(machineInput, 0, RE2Flags.ANCHOR_BOTH, 0) !== null;
|
|
6366
6366
|
}
|
|
6367
6367
|
/**
|
|
6368
|
+
* Executes a search for a match in a specified string.
|
|
6369
|
+
* Returns a result array, or null if no match is found.
|
|
6370
|
+
* The returned array perfectly mirrors standard JavaScript `RegExpExecArray`,
|
|
6371
|
+
* including `.index`, `.input`, and `.groups` properties.
|
|
6372
|
+
*
|
|
6373
|
+
* @param {string|number[]|Uint8Array} input the input string or byte array
|
|
6374
|
+
* @returns {Array|null} the match array with index, input, and groups properties, or null
|
|
6375
|
+
*/
|
|
6376
|
+
exec(input) {
|
|
6377
|
+
const m = this.matcher(input);
|
|
6378
|
+
if (!m.find()) return null;
|
|
6379
|
+
const result = [m.group(0)];
|
|
6380
|
+
for (let i = 1; i <= m.groupCount(); i++) {
|
|
6381
|
+
const val = m.group(i);
|
|
6382
|
+
result.push(val === null ? void 0 : val);
|
|
6383
|
+
}
|
|
6384
|
+
result.index = m.start(0);
|
|
6385
|
+
result.input = input;
|
|
6386
|
+
const namedGroups = this.namedGroups();
|
|
6387
|
+
if (Object.keys(namedGroups).length > 0) {
|
|
6388
|
+
const parsedGroups = m.getNamedGroups();
|
|
6389
|
+
for (const key in parsedGroups) if (parsedGroups[key] === null) parsedGroups[key] = void 0;
|
|
6390
|
+
result.groups = parsedGroups;
|
|
6391
|
+
} else result.groups = void 0;
|
|
6392
|
+
return result;
|
|
6393
|
+
}
|
|
6394
|
+
/**
|
|
6368
6395
|
* Splits input around instances of the regular expression. It returns an array giving the strings
|
|
6369
6396
|
* that occur before, between, and after instances of the regular expression.
|
|
6370
6397
|
*
|
|
@@ -6416,7 +6443,7 @@ var RE2JS = class RE2JS {
|
|
|
6416
6443
|
* including capturing groups.
|
|
6417
6444
|
*
|
|
6418
6445
|
* @param {string|number[]|Uint8Array} input the input string or byte array
|
|
6419
|
-
* @returns {IterableIterator<
|
|
6446
|
+
* @returns {IterableIterator<RegExpMatchArray>}
|
|
6420
6447
|
*/
|
|
6421
6448
|
*matchAll(input) {
|
|
6422
6449
|
const m = this.matcher(input);
|
package/build/index.umd.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* re2js
|
|
3
3
|
* RE2JS is the JavaScript port of RE2, a regular expression engine that provides linear time matching
|
|
4
4
|
*
|
|
5
|
-
* @version v2.8.
|
|
5
|
+
* @version v2.8.5
|
|
6
6
|
* @author Oleksii Vasyliev
|
|
7
7
|
* @homepage https://github.com/le0pard/re2js#readme
|
|
8
8
|
* @repository github:le0pard/re2js
|
|
@@ -1249,7 +1249,7 @@
|
|
|
1249
1249
|
}
|
|
1250
1250
|
/**
|
|
1251
1251
|
* Resets the {@code Matcher} and changes the input.
|
|
1252
|
-
* @param {MatcherInputBase} input
|
|
1252
|
+
* @param {string|number[]|Uint8Array|MatcherInputBase} input
|
|
1253
1253
|
* @returns {Matcher} the {@code Matcher} itself, for chained method calls
|
|
1254
1254
|
*/
|
|
1255
1255
|
resetMatcherInput(input) {
|
|
@@ -6369,6 +6369,33 @@
|
|
|
6369
6369
|
return this.re2Input.executeEngine(machineInput, 0, RE2Flags.ANCHOR_BOTH, 0) !== null;
|
|
6370
6370
|
}
|
|
6371
6371
|
/**
|
|
6372
|
+
* Executes a search for a match in a specified string.
|
|
6373
|
+
* Returns a result array, or null if no match is found.
|
|
6374
|
+
* The returned array perfectly mirrors standard JavaScript `RegExpExecArray`,
|
|
6375
|
+
* including `.index`, `.input`, and `.groups` properties.
|
|
6376
|
+
*
|
|
6377
|
+
* @param {string|number[]|Uint8Array} input the input string or byte array
|
|
6378
|
+
* @returns {Array|null} the match array with index, input, and groups properties, or null
|
|
6379
|
+
*/
|
|
6380
|
+
exec(input) {
|
|
6381
|
+
const m = this.matcher(input);
|
|
6382
|
+
if (!m.find()) return null;
|
|
6383
|
+
const result = [m.group(0)];
|
|
6384
|
+
for (let i = 1; i <= m.groupCount(); i++) {
|
|
6385
|
+
const val = m.group(i);
|
|
6386
|
+
result.push(val === null ? void 0 : val);
|
|
6387
|
+
}
|
|
6388
|
+
result.index = m.start(0);
|
|
6389
|
+
result.input = input;
|
|
6390
|
+
const namedGroups = this.namedGroups();
|
|
6391
|
+
if (Object.keys(namedGroups).length > 0) {
|
|
6392
|
+
const parsedGroups = m.getNamedGroups();
|
|
6393
|
+
for (const key in parsedGroups) if (parsedGroups[key] === null) parsedGroups[key] = void 0;
|
|
6394
|
+
result.groups = parsedGroups;
|
|
6395
|
+
} else result.groups = void 0;
|
|
6396
|
+
return result;
|
|
6397
|
+
}
|
|
6398
|
+
/**
|
|
6372
6399
|
* Splits input around instances of the regular expression. It returns an array giving the strings
|
|
6373
6400
|
* that occur before, between, and after instances of the regular expression.
|
|
6374
6401
|
*
|
|
@@ -6420,7 +6447,7 @@
|
|
|
6420
6447
|
* including capturing groups.
|
|
6421
6448
|
*
|
|
6422
6449
|
* @param {string|number[]|Uint8Array} input the input string or byte array
|
|
6423
|
-
* @returns {IterableIterator<
|
|
6450
|
+
* @returns {IterableIterator<RegExpMatchArray>}
|
|
6424
6451
|
*/
|
|
6425
6452
|
*matchAll(input) {
|
|
6426
6453
|
const m = this.matcher(input);
|