rregex 1.10.1 → 1.10.3
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 +6 -294
- package/lib/browser.d.ts +164 -18
- package/lib/browser.js +115 -46
- package/lib/commonjs.d.ts +164 -18
- package/lib/commonjs.js +115 -46
- package/lib/module.d.ts +164 -18
- package/lib/module.js +116 -47
- package/lib/rregex.wasm +0 -0
- package/lib/types.d.ts +164 -18
- package/package.json +1 -1
package/lib/commonjs.js
CHANGED
|
@@ -3,6 +3,24 @@ imports['wbg'] = module.exports;
|
|
|
3
3
|
let wasm;
|
|
4
4
|
const { TextDecoder, TextEncoder } = require(`util`);
|
|
5
5
|
|
|
6
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
7
|
+
|
|
8
|
+
cachedTextDecoder.decode();
|
|
9
|
+
|
|
10
|
+
let cachedUint8Memory0 = null;
|
|
11
|
+
|
|
12
|
+
function getUint8Memory0() {
|
|
13
|
+
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
|
|
14
|
+
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
|
|
15
|
+
}
|
|
16
|
+
return cachedUint8Memory0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function getStringFromWasm0(ptr, len) {
|
|
20
|
+
ptr = ptr >>> 0;
|
|
21
|
+
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
|
|
22
|
+
}
|
|
23
|
+
|
|
6
24
|
const heap = new Array(128).fill(undefined);
|
|
7
25
|
|
|
8
26
|
heap.push(undefined, null, true, false);
|
|
@@ -32,24 +50,6 @@ function takeObject(idx) {
|
|
|
32
50
|
return ret;
|
|
33
51
|
}
|
|
34
52
|
|
|
35
|
-
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
36
|
-
|
|
37
|
-
cachedTextDecoder.decode();
|
|
38
|
-
|
|
39
|
-
let cachedUint8Memory0 = null;
|
|
40
|
-
|
|
41
|
-
function getUint8Memory0() {
|
|
42
|
-
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
|
|
43
|
-
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
|
|
44
|
-
}
|
|
45
|
-
return cachedUint8Memory0;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function getStringFromWasm0(ptr, len) {
|
|
49
|
-
ptr = ptr >>> 0;
|
|
50
|
-
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
53
|
let WASM_VECTOR_LEN = 0;
|
|
54
54
|
|
|
55
55
|
let cachedTextEncoder = new TextEncoder('utf-8');
|
|
@@ -181,6 +181,43 @@ function handleError(f, args) {
|
|
|
181
181
|
}
|
|
182
182
|
/**
|
|
183
183
|
* A compiled regular expression for matching Unicode strings.
|
|
184
|
+
*
|
|
185
|
+
* A `RRegex` can be used to search haystacks, split haystacks into substrings
|
|
186
|
+
* or replace substrings in a haystack with a different substring. All
|
|
187
|
+
* searching is done with an implicit `(?s:.)*?` at the beginning and end of
|
|
188
|
+
* an pattern. To force an expression to match the whole string (or a prefix
|
|
189
|
+
* or a suffix), you must use an anchor like `^` or `$` (or `\A` and `\z`).
|
|
190
|
+
*
|
|
191
|
+
* While this library will handle Unicode strings (whether in the regular
|
|
192
|
+
* expression or in the haystack), all positions returned are **byte
|
|
193
|
+
* offsets**. Every byte offset is guaranteed to be at a Unicode code point
|
|
194
|
+
* boundary. That is, all offsets returned by the `RRegex` API are guaranteed
|
|
195
|
+
* to be ranges that can slice an `Uint8Array` created with a TextEncoder.
|
|
196
|
+
*
|
|
197
|
+
* # Example: slicing over UTF strings
|
|
198
|
+
*
|
|
199
|
+
* ```typescript
|
|
200
|
+
* import { RRegex } from "rregex"
|
|
201
|
+
*
|
|
202
|
+
* const re = new RRegex("ä")
|
|
203
|
+
* const m = re.find("äöü") // { start: 0, end: 2 }
|
|
204
|
+
*
|
|
205
|
+
* const buff = new TextEncoder().encode("äöü")
|
|
206
|
+
* const slice = buff.slice(m.start, m.end)
|
|
207
|
+
* expect(new TextDecoder().decode(slice)).toBe("ä")
|
|
208
|
+
* ```
|
|
209
|
+
*
|
|
210
|
+
* # Example: find the offsets of a US phone number:
|
|
211
|
+
*
|
|
212
|
+
* ```typescript
|
|
213
|
+
* import { RRegex } from "rregex"
|
|
214
|
+
*
|
|
215
|
+
* const re = new RRegex("[0-9]{3}-[0-9]{3}-[0-9]{4}");
|
|
216
|
+
* const m = re.find("phone: 111-222-3333");
|
|
217
|
+
* expect(m.start).toBe(7);
|
|
218
|
+
* expect(m.end).toBe(19);
|
|
219
|
+
* ```
|
|
220
|
+
*
|
|
184
221
|
* @see https://docs.rs/regex/latest/regex/
|
|
185
222
|
*/
|
|
186
223
|
class RRegex {
|
|
@@ -235,6 +272,8 @@ class RRegex {
|
|
|
235
272
|
* Unicode word characters:
|
|
236
273
|
*
|
|
237
274
|
* ```typescript
|
|
275
|
+
* import { RRegex } from "rregex"
|
|
276
|
+
*
|
|
238
277
|
* const text = "I categorically deny having triskaidekaphobia."
|
|
239
278
|
* expect(new RRegex("\\b\\w{13}\\b").is_match(text)).toBe(true)
|
|
240
279
|
* ```
|
|
@@ -280,8 +319,10 @@ class RRegex {
|
|
|
280
319
|
* Unicode word characters:
|
|
281
320
|
*
|
|
282
321
|
* ```typescript
|
|
322
|
+
* import { RRegex } from "rregex"
|
|
323
|
+
*
|
|
283
324
|
* const text = "I categorically deny having triskaidekaphobia."
|
|
284
|
-
* const m = new
|
|
325
|
+
* const m = new RRegex("\\b\\w{13}\\b").find(text)
|
|
285
326
|
* expect(m.start).toBe(2)
|
|
286
327
|
* expect(m.end).toBe(15)
|
|
287
328
|
* ```
|
|
@@ -347,8 +388,10 @@ class RRegex {
|
|
|
347
388
|
* word characters:
|
|
348
389
|
*
|
|
349
390
|
* ```typescript
|
|
391
|
+
* import { RRegex } from "rregex"
|
|
392
|
+
*
|
|
350
393
|
* const text = "Retroactively relinquishing remunerations is reprehensible."
|
|
351
|
-
* const matches = new
|
|
394
|
+
* const matches = new RRegex("\\b\\w{13}\\b").findAll(text)
|
|
352
395
|
* ```
|
|
353
396
|
*
|
|
354
397
|
* @see https://docs.rs/regex/latest/regex/struct.Regex.html#method.find_iter
|
|
@@ -407,7 +450,9 @@ class RRegex {
|
|
|
407
450
|
* year separately.
|
|
408
451
|
*
|
|
409
452
|
* ```typescript
|
|
410
|
-
*
|
|
453
|
+
* import { RRegex } from "rregex"
|
|
454
|
+
*
|
|
455
|
+
* const re = new RRegex("'([^']+)'\\s+\\((\\d{4})\)")
|
|
411
456
|
* const text = "Not my favorite movie: 'Citizen Kane' (1941)."
|
|
412
457
|
* const caps = re.captures(text)
|
|
413
458
|
* expect(caps.get[1].value).toBe("Citizen Kane")
|
|
@@ -421,7 +466,9 @@ class RRegex {
|
|
|
421
466
|
* We can make this example a bit clearer by using *named* capture groups:
|
|
422
467
|
*
|
|
423
468
|
* ```typescript
|
|
424
|
-
*
|
|
469
|
+
* import { RRegex } from "rregex"
|
|
470
|
+
*
|
|
471
|
+
* const re = new RRegex("'(?P<title>[^']+)'\\s+\\((?P<year>\\d{4})\)")
|
|
425
472
|
* const text = "Not my favorite movie: 'Citizen Kane' (1941)."
|
|
426
473
|
* const caps = re.captures(text)
|
|
427
474
|
* expect(caps.name["title"].value).toBe("Citizen Kane")
|
|
@@ -461,7 +508,9 @@ class RRegex {
|
|
|
461
508
|
* some text, where the movie is formatted like "'Title' (xxxx)":
|
|
462
509
|
*
|
|
463
510
|
* ```typescript
|
|
464
|
-
*
|
|
511
|
+
* import { RRegex } from "rregex"
|
|
512
|
+
*
|
|
513
|
+
* const re = new RRegex("'(?P<title>[^']+)'\\s+\\((?P<year>\\d{4})\\)")
|
|
465
514
|
* const text = "'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931)."
|
|
466
515
|
* for caps of re.captures_iter(text) {
|
|
467
516
|
* console.log(
|
|
@@ -508,6 +557,8 @@ class RRegex {
|
|
|
508
557
|
* ## Example
|
|
509
558
|
*
|
|
510
559
|
* ```typescript
|
|
560
|
+
* import { RRegex } from "rregex"
|
|
561
|
+
*
|
|
511
562
|
* const re1 = new RRegex("(?P<y>\\d{4})-(?P<m>\\d{2})-(?P<d>\\d{2})")
|
|
512
563
|
* expect(re1.capturesLength()).toEqual(4)
|
|
513
564
|
*
|
|
@@ -564,7 +615,9 @@ class RRegex {
|
|
|
564
615
|
* In typical usage, this can just be a normal string:
|
|
565
616
|
*
|
|
566
617
|
* ```typescript
|
|
567
|
-
*
|
|
618
|
+
* import { RRegex } from "rregex"
|
|
619
|
+
*
|
|
620
|
+
* const re = new RRegex("[^01]+")
|
|
568
621
|
* expect(re.replace("1078910", "").toBe("1010")
|
|
569
622
|
* ```
|
|
570
623
|
*
|
|
@@ -572,7 +625,9 @@ class RRegex {
|
|
|
572
625
|
* groups:
|
|
573
626
|
*
|
|
574
627
|
* ```typescript
|
|
575
|
-
*
|
|
628
|
+
* import { RRegex } from "rregex"
|
|
629
|
+
*
|
|
630
|
+
* const re = new RRegex("(?P<last>[^,\\s]+),\\s+(?P<first>\\S+)")
|
|
576
631
|
* const result = re.replace("Springsteen, Bruce", "$first $last")
|
|
577
632
|
* expect(result).toBe("Bruce Springsteen")
|
|
578
633
|
* ```
|
|
@@ -586,7 +641,9 @@ class RRegex {
|
|
|
586
641
|
* underscore:
|
|
587
642
|
*
|
|
588
643
|
* ```typescript
|
|
589
|
-
*
|
|
644
|
+
* import { RRegex } from "rregex"
|
|
645
|
+
*
|
|
646
|
+
* const re = new RRegex("(?P<first>\\w+)\\s+(?P<second>\\w+)")
|
|
590
647
|
* const result = re.replace("deep fried", "${first}_$second")
|
|
591
648
|
* expect(result).toBe("deep_fried")
|
|
592
649
|
* ```
|
|
@@ -700,7 +757,9 @@ class RRegex {
|
|
|
700
757
|
* To split a string delimited by arbitrary amounts of spaces or tabs:
|
|
701
758
|
*
|
|
702
759
|
* ```typescript
|
|
703
|
-
*
|
|
760
|
+
* import { RRegex } from "rregex"
|
|
761
|
+
*
|
|
762
|
+
* const re = new RRegex(r"[ \\t]+")
|
|
704
763
|
* const fields = re.split("a b \t c\td e")
|
|
705
764
|
* expect(fields).toEqual(["a", "b", "c", "d", "e"])
|
|
706
765
|
* ```
|
|
@@ -738,7 +797,9 @@ class RRegex {
|
|
|
738
797
|
* Get the first two words in some text:
|
|
739
798
|
*
|
|
740
799
|
* ```typescript
|
|
741
|
-
*
|
|
800
|
+
* import { RRegex } from "rregex"
|
|
801
|
+
*
|
|
802
|
+
* const re = new RRegex(r"\\W+")
|
|
742
803
|
* const fields = re.splitn("Hey! How are you?", 3)
|
|
743
804
|
* expect(fields).toEqual(["Hey", "How", "are you?"])
|
|
744
805
|
* ```
|
|
@@ -784,8 +845,10 @@ class RRegex {
|
|
|
784
845
|
* `a`.
|
|
785
846
|
*
|
|
786
847
|
* ```typescript
|
|
848
|
+
* import { RRegex } from "rregex"
|
|
849
|
+
*
|
|
787
850
|
* const text = "aaaaa"
|
|
788
|
-
* const = new
|
|
851
|
+
* const = new RRegex("a+").shortest_match(text)
|
|
789
852
|
* expect(pos).toBe(1)
|
|
790
853
|
* ```
|
|
791
854
|
*
|
|
@@ -909,6 +972,8 @@ class RRegexSet {
|
|
|
909
972
|
* Create a new regex set from an iterator of strings:
|
|
910
973
|
*
|
|
911
974
|
* ```typescript
|
|
975
|
+
* import { RegexSet } from "rregex"
|
|
976
|
+
*
|
|
912
977
|
* const set = new RegexSet(["\\w+", "\\d+"])
|
|
913
978
|
* expect(set.is_match("foo")).toBe(true)
|
|
914
979
|
* ```
|
|
@@ -951,6 +1016,8 @@ class RRegexSet {
|
|
|
951
1016
|
* Tests whether a set matches some text:
|
|
952
1017
|
*
|
|
953
1018
|
* ```typescript
|
|
1019
|
+
* import { RegexSet } from "rregex"
|
|
1020
|
+
*
|
|
954
1021
|
* const set = new RegexSet(["\\w+", "\\d+"])
|
|
955
1022
|
* expect(set.is_match("foo")).toBe(true)
|
|
956
1023
|
* expect(!set.is_match("☃")).toBe(false)
|
|
@@ -985,6 +1052,8 @@ class RRegexSet {
|
|
|
985
1052
|
* Tests which regular expressions match the given text:
|
|
986
1053
|
*
|
|
987
1054
|
* ```typescript
|
|
1055
|
+
* import { RegexSet } from "rregex"
|
|
1056
|
+
*
|
|
988
1057
|
* const set = new RegexSet([
|
|
989
1058
|
* "\\w+",
|
|
990
1059
|
* "\\d+",
|
|
@@ -1020,8 +1089,8 @@ class RRegexSet {
|
|
|
1020
1089
|
}
|
|
1021
1090
|
module.exports.RRegexSet = RRegexSet;
|
|
1022
1091
|
|
|
1023
|
-
module.exports.
|
|
1024
|
-
const ret = arg0;
|
|
1092
|
+
module.exports.__wbindgen_string_new = function(arg0, arg1) {
|
|
1093
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
1025
1094
|
return addHeapObject(ret);
|
|
1026
1095
|
};
|
|
1027
1096
|
|
|
@@ -1029,8 +1098,8 @@ module.exports.__wbindgen_object_drop_ref = function(arg0) {
|
|
|
1029
1098
|
takeObject(arg0);
|
|
1030
1099
|
};
|
|
1031
1100
|
|
|
1032
|
-
module.exports.
|
|
1033
|
-
const ret =
|
|
1101
|
+
module.exports.__wbindgen_number_new = function(arg0) {
|
|
1102
|
+
const ret = arg0;
|
|
1034
1103
|
return addHeapObject(ret);
|
|
1035
1104
|
};
|
|
1036
1105
|
|
|
@@ -1062,61 +1131,61 @@ module.exports.__wbg_set_9182712abebf82ef = function(arg0, arg1, arg2) {
|
|
|
1062
1131
|
getObject(arg0)[takeObject(arg1)] = takeObject(arg2);
|
|
1063
1132
|
};
|
|
1064
1133
|
|
|
1065
|
-
module.exports.
|
|
1134
|
+
module.exports.__wbg_get_c43534c00f382c8a = function(arg0, arg1) {
|
|
1066
1135
|
const ret = getObject(arg0)[arg1 >>> 0];
|
|
1067
1136
|
return addHeapObject(ret);
|
|
1068
1137
|
};
|
|
1069
1138
|
|
|
1070
|
-
module.exports.
|
|
1139
|
+
module.exports.__wbg_length_d99b680fd68bf71b = function(arg0) {
|
|
1071
1140
|
const ret = getObject(arg0).length;
|
|
1072
1141
|
return ret;
|
|
1073
1142
|
};
|
|
1074
1143
|
|
|
1075
|
-
module.exports.
|
|
1144
|
+
module.exports.__wbg_new_34c624469fb1d4fd = function() {
|
|
1076
1145
|
const ret = new Array();
|
|
1077
1146
|
return addHeapObject(ret);
|
|
1078
1147
|
};
|
|
1079
1148
|
|
|
1080
|
-
module.exports.
|
|
1149
|
+
module.exports.__wbg_new_87d841e70661f6e9 = function() {
|
|
1081
1150
|
const ret = new Object();
|
|
1082
1151
|
return addHeapObject(ret);
|
|
1083
1152
|
};
|
|
1084
1153
|
|
|
1085
|
-
module.exports.
|
|
1154
|
+
module.exports.__wbg_set_379b27f1d5f1bf9c = function(arg0, arg1, arg2) {
|
|
1086
1155
|
getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
|
|
1087
1156
|
};
|
|
1088
1157
|
|
|
1089
|
-
module.exports.
|
|
1158
|
+
module.exports.__wbg_from_a663e01d8dab8e44 = function(arg0) {
|
|
1090
1159
|
const ret = Array.from(getObject(arg0));
|
|
1091
1160
|
return addHeapObject(ret);
|
|
1092
1161
|
};
|
|
1093
1162
|
|
|
1094
|
-
module.exports.
|
|
1163
|
+
module.exports.__wbg_push_906164999551d793 = function(arg0, arg1) {
|
|
1095
1164
|
const ret = getObject(arg0).push(getObject(arg1));
|
|
1096
1165
|
return ret;
|
|
1097
1166
|
};
|
|
1098
1167
|
|
|
1099
|
-
module.exports.
|
|
1168
|
+
module.exports.__wbg_fromCodePoint_33fc13cdbfe3bca7 = function() { return handleError(function (arg0) {
|
|
1100
1169
|
const ret = String.fromCodePoint(arg0 >>> 0);
|
|
1101
1170
|
return addHeapObject(ret);
|
|
1102
1171
|
}, arguments) };
|
|
1103
1172
|
|
|
1104
|
-
module.exports.
|
|
1173
|
+
module.exports.__wbg_buffer_5d1b598a01b41a42 = function(arg0) {
|
|
1105
1174
|
const ret = getObject(arg0).buffer;
|
|
1106
1175
|
return addHeapObject(ret);
|
|
1107
1176
|
};
|
|
1108
1177
|
|
|
1109
|
-
module.exports.
|
|
1178
|
+
module.exports.__wbg_newwithbyteoffsetandlength_d695c7957788f922 = function(arg0, arg1, arg2) {
|
|
1110
1179
|
const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
|
|
1111
1180
|
return addHeapObject(ret);
|
|
1112
1181
|
};
|
|
1113
1182
|
|
|
1114
|
-
module.exports.
|
|
1183
|
+
module.exports.__wbg_new_ace717933ad7117f = function(arg0) {
|
|
1115
1184
|
const ret = new Uint8Array(getObject(arg0));
|
|
1116
1185
|
return addHeapObject(ret);
|
|
1117
1186
|
};
|
|
1118
1187
|
|
|
1119
|
-
module.exports.
|
|
1188
|
+
module.exports.__wbg_set_37a50e901587b477 = function() { return handleError(function (arg0, arg1, arg2) {
|
|
1120
1189
|
const ret = Reflect.set(getObject(arg0), getObject(arg1), getObject(arg2));
|
|
1121
1190
|
return ret;
|
|
1122
1191
|
}, arguments) };
|
|
@@ -1147,6 +1216,6 @@ module.exports.metadata = {
|
|
|
1147
1216
|
],
|
|
1148
1217
|
"homepage": "http://rregex.dev",
|
|
1149
1218
|
"repository": "https://github.com/2fd/rregex",
|
|
1150
|
-
"regex": "1.10.
|
|
1219
|
+
"regex": "1.10.3",
|
|
1151
1220
|
"regex-syntax": "0.8.2"
|
|
1152
1221
|
}
|
package/lib/module.d.ts
CHANGED
|
@@ -11,6 +11,50 @@
|
|
|
11
11
|
export function escape(text: string): string;
|
|
12
12
|
/**
|
|
13
13
|
* Represents a single match of a regex in a haystack.
|
|
14
|
+
*
|
|
15
|
+
* A `Match` contains both the start and end byte offsets of the match and the
|
|
16
|
+
* actual substring corresponding to the range of those byte offsets. It is
|
|
17
|
+
* guaranteed that `start <= end`. When `start == end`, the match is empty.
|
|
18
|
+
*
|
|
19
|
+
* Since this `Match` can only be produced by the top-level `Regex` APIs
|
|
20
|
+
* that only support searching UTF-8 encoded strings, the byte offsets for a
|
|
21
|
+
* `Match` are guaranteed to fall on valid UTF-8 codepoint boundaries. That
|
|
22
|
+
* is, slicing an `Uint8Array` created with a TextEncoder is guaranteed to never
|
|
23
|
+
* be out of range.
|
|
24
|
+
*
|
|
25
|
+
* # Numbering
|
|
26
|
+
*
|
|
27
|
+
* The byte offsets in a `Match` form a half-open interval. That is, the
|
|
28
|
+
* start of the range is inclusive and the end of the range is exclusive.
|
|
29
|
+
* For example, given a haystack `abcFOOxyz` and a match of `FOO`, its byte
|
|
30
|
+
* offset range starts at `3` and ends at `6`. `3` corresponds to `F` and
|
|
31
|
+
* `6` corresponds to `x`, which is one past the end of the match. This
|
|
32
|
+
* corresponds to the same kind of slicing that Rust uses.
|
|
33
|
+
*
|
|
34
|
+
* For more on why this was chosen over other schemes (aside from being
|
|
35
|
+
* consistent with how Rust the language works), see [this discussion] and
|
|
36
|
+
* [Dijkstra's note on a related topic][note].
|
|
37
|
+
*
|
|
38
|
+
* [this discussion]: https://github.com/rust-lang/regex/discussions/866
|
|
39
|
+
* [note]: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
|
|
40
|
+
*
|
|
41
|
+
* # Example
|
|
42
|
+
*
|
|
43
|
+
* This example shows the value of each of the methods on `Match` for a
|
|
44
|
+
* particular search.
|
|
45
|
+
*
|
|
46
|
+
* ```typescript
|
|
47
|
+
* import { RRegex } from "rregex"
|
|
48
|
+
*
|
|
49
|
+
* const re = new RRegex("\\p{Greek}+");
|
|
50
|
+
* const hay = "Greek: αβγδ";
|
|
51
|
+
* const m = re.find(hay);
|
|
52
|
+
* expect(m.start).toBe(7);
|
|
53
|
+
* expect(m.end).toBe(15);
|
|
54
|
+
* expect(m.value).toBe("αβγδ");
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @see @see https://docs.rs/regex/latest/regex/
|
|
14
58
|
*/
|
|
15
59
|
export type Match = {
|
|
16
60
|
start: number
|
|
@@ -19,14 +63,47 @@ export type Match = {
|
|
|
19
63
|
}
|
|
20
64
|
|
|
21
65
|
/**
|
|
22
|
-
*
|
|
66
|
+
* Represents the capture groups for a single match.
|
|
67
|
+
*
|
|
68
|
+
* Capture groups refer to parts of a regex enclosed in parentheses. They can
|
|
69
|
+
* be optionally named. The purpose of capture groups is to be able to
|
|
70
|
+
* reference different parts of a match based on the original pattern. For
|
|
71
|
+
* example, say you want to match the individual letters in a 5-letter word:
|
|
23
72
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* 0th capture is always unnamed and so must be accessed with the get property.)
|
|
73
|
+
* ```text
|
|
74
|
+
* (?<first>\w)(\w)(?:\w)\w(?<last>\w)
|
|
75
|
+
* ```
|
|
28
76
|
*
|
|
29
|
-
*
|
|
77
|
+
* This regex has 4 capture groups:
|
|
78
|
+
*
|
|
79
|
+
* * The group at index `0` corresponds to the overall match. It is always
|
|
80
|
+
* present in every match and never has a name.
|
|
81
|
+
* * The group at index `1` with name `first` corresponding to the first
|
|
82
|
+
* letter.
|
|
83
|
+
* * The group at index `2` with no name corresponding to the second letter.
|
|
84
|
+
* * The group at index `3` with name `last` corresponding to the fifth and
|
|
85
|
+
* last letter.
|
|
86
|
+
*
|
|
87
|
+
* Notice that `(?:\w)` was not listed above as a capture group despite it
|
|
88
|
+
* being enclosed in parentheses. That's because `(?:pattern)` is a special
|
|
89
|
+
* syntax that permits grouping but *without* capturing. The reason for not
|
|
90
|
+
* treating it as a capture is that tracking and reporting capture groups
|
|
91
|
+
* requires additional state that may lead to slower searches. So using as few
|
|
92
|
+
* capture groups as possible can help performance. (Although the difference
|
|
93
|
+
* in performance of a couple of capture groups is likely immaterial.)
|
|
94
|
+
*
|
|
95
|
+
* # Example
|
|
96
|
+
*
|
|
97
|
+
* ```typescript
|
|
98
|
+
* import { RRegex } from "rregex"
|
|
99
|
+
*
|
|
100
|
+
* const re = new RRegex("(?<first>\\w)(\\w)(?:\\w)\\w(?<last>\\w)");
|
|
101
|
+
* const caps = re.captures("toady");
|
|
102
|
+
* expect(caps.get[0].value).toBe("toady");
|
|
103
|
+
* expect(caps.name["first"].value).toBe("t");
|
|
104
|
+
* expect(caps.get[2].value).toBe("o");
|
|
105
|
+
* expect(caps.name["last"].value).toBe("y");
|
|
106
|
+
* ```
|
|
30
107
|
*/
|
|
31
108
|
export type Captures = {
|
|
32
109
|
get: Match[]
|
|
@@ -584,6 +661,43 @@ export type Capture = {
|
|
|
584
661
|
|
|
585
662
|
/**
|
|
586
663
|
* A compiled regular expression for matching Unicode strings.
|
|
664
|
+
*
|
|
665
|
+
* A `RRegex` can be used to search haystacks, split haystacks into substrings
|
|
666
|
+
* or replace substrings in a haystack with a different substring. All
|
|
667
|
+
* searching is done with an implicit `(?s:.)*?` at the beginning and end of
|
|
668
|
+
* an pattern. To force an expression to match the whole string (or a prefix
|
|
669
|
+
* or a suffix), you must use an anchor like `^` or `$` (or `\A` and `\z`).
|
|
670
|
+
*
|
|
671
|
+
* While this library will handle Unicode strings (whether in the regular
|
|
672
|
+
* expression or in the haystack), all positions returned are **byte
|
|
673
|
+
* offsets**. Every byte offset is guaranteed to be at a Unicode code point
|
|
674
|
+
* boundary. That is, all offsets returned by the `RRegex` API are guaranteed
|
|
675
|
+
* to be ranges that can slice an `Uint8Array` created with a TextEncoder.
|
|
676
|
+
*
|
|
677
|
+
* # Example: slicing over UTF strings
|
|
678
|
+
*
|
|
679
|
+
* ```typescript
|
|
680
|
+
* import { RRegex } from "rregex"
|
|
681
|
+
*
|
|
682
|
+
* const re = new RRegex("ä")
|
|
683
|
+
* const m = re.find("äöü") // { start: 0, end: 2 }
|
|
684
|
+
*
|
|
685
|
+
* const buff = new TextEncoder().encode("äöü")
|
|
686
|
+
* const slice = buff.slice(m.start, m.end)
|
|
687
|
+
* expect(new TextDecoder().decode(slice)).toBe("ä")
|
|
688
|
+
* ```
|
|
689
|
+
*
|
|
690
|
+
* # Example: find the offsets of a US phone number:
|
|
691
|
+
*
|
|
692
|
+
* ```typescript
|
|
693
|
+
* import { RRegex } from "rregex"
|
|
694
|
+
*
|
|
695
|
+
* const re = new RRegex("[0-9]{3}-[0-9]{3}-[0-9]{4}");
|
|
696
|
+
* const m = re.find("phone: 111-222-3333");
|
|
697
|
+
* expect(m.start).toBe(7);
|
|
698
|
+
* expect(m.end).toBe(19);
|
|
699
|
+
* ```
|
|
700
|
+
*
|
|
587
701
|
* @see https://docs.rs/regex/latest/regex/
|
|
588
702
|
*/
|
|
589
703
|
export class RRegex {
|
|
@@ -610,6 +724,8 @@ export class RRegex {
|
|
|
610
724
|
* Unicode word characters:
|
|
611
725
|
*
|
|
612
726
|
* ```typescript
|
|
727
|
+
* import { RRegex } from "rregex"
|
|
728
|
+
*
|
|
613
729
|
* const text = "I categorically deny having triskaidekaphobia."
|
|
614
730
|
* expect(new RRegex("\\b\\w{13}\\b").is_match(text)).toBe(true)
|
|
615
731
|
* ```
|
|
@@ -645,8 +761,10 @@ export class RRegex {
|
|
|
645
761
|
* Unicode word characters:
|
|
646
762
|
*
|
|
647
763
|
* ```typescript
|
|
764
|
+
* import { RRegex } from "rregex"
|
|
765
|
+
*
|
|
648
766
|
* const text = "I categorically deny having triskaidekaphobia."
|
|
649
|
-
* const m = new
|
|
767
|
+
* const m = new RRegex("\\b\\w{13}\\b").find(text)
|
|
650
768
|
* expect(m.start).toBe(2)
|
|
651
769
|
* expect(m.end).toBe(15)
|
|
652
770
|
* ```
|
|
@@ -680,8 +798,10 @@ export class RRegex {
|
|
|
680
798
|
* word characters:
|
|
681
799
|
*
|
|
682
800
|
* ```typescript
|
|
801
|
+
* import { RRegex } from "rregex"
|
|
802
|
+
*
|
|
683
803
|
* const text = "Retroactively relinquishing remunerations is reprehensible."
|
|
684
|
-
* const matches = new
|
|
804
|
+
* const matches = new RRegex("\\b\\w{13}\\b").findAll(text)
|
|
685
805
|
* ```
|
|
686
806
|
*
|
|
687
807
|
* @see https://docs.rs/regex/latest/regex/struct.Regex.html#method.find_iter
|
|
@@ -712,7 +832,9 @@ export class RRegex {
|
|
|
712
832
|
* year separately.
|
|
713
833
|
*
|
|
714
834
|
* ```typescript
|
|
715
|
-
*
|
|
835
|
+
* import { RRegex } from "rregex"
|
|
836
|
+
*
|
|
837
|
+
* const re = new RRegex("'([^']+)'\\s+\\((\\d{4})\)")
|
|
716
838
|
* const text = "Not my favorite movie: 'Citizen Kane' (1941)."
|
|
717
839
|
* const caps = re.captures(text)
|
|
718
840
|
* expect(caps.get[1].value).toBe("Citizen Kane")
|
|
@@ -726,7 +848,9 @@ export class RRegex {
|
|
|
726
848
|
* We can make this example a bit clearer by using *named* capture groups:
|
|
727
849
|
*
|
|
728
850
|
* ```typescript
|
|
729
|
-
*
|
|
851
|
+
* import { RRegex } from "rregex"
|
|
852
|
+
*
|
|
853
|
+
* const re = new RRegex("'(?P<title>[^']+)'\\s+\\((?P<year>\\d{4})\)")
|
|
730
854
|
* const text = "Not my favorite movie: 'Citizen Kane' (1941)."
|
|
731
855
|
* const caps = re.captures(text)
|
|
732
856
|
* expect(caps.name["title"].value).toBe("Citizen Kane")
|
|
@@ -750,7 +874,9 @@ export class RRegex {
|
|
|
750
874
|
* some text, where the movie is formatted like "'Title' (xxxx)":
|
|
751
875
|
*
|
|
752
876
|
* ```typescript
|
|
753
|
-
*
|
|
877
|
+
* import { RRegex } from "rregex"
|
|
878
|
+
*
|
|
879
|
+
* const re = new RRegex("'(?P<title>[^']+)'\\s+\\((?P<year>\\d{4})\\)")
|
|
754
880
|
* const text = "'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931)."
|
|
755
881
|
* for caps of re.captures_iter(text) {
|
|
756
882
|
* console.log(
|
|
@@ -781,6 +907,8 @@ export class RRegex {
|
|
|
781
907
|
* ## Example
|
|
782
908
|
*
|
|
783
909
|
* ```typescript
|
|
910
|
+
* import { RRegex } from "rregex"
|
|
911
|
+
*
|
|
784
912
|
* const re1 = new RRegex("(?P<y>\\d{4})-(?P<m>\\d{2})-(?P<d>\\d{2})")
|
|
785
913
|
* expect(re1.capturesLength()).toEqual(4)
|
|
786
914
|
*
|
|
@@ -834,7 +962,9 @@ export class RRegex {
|
|
|
834
962
|
* In typical usage, this can just be a normal string:
|
|
835
963
|
*
|
|
836
964
|
* ```typescript
|
|
837
|
-
*
|
|
965
|
+
* import { RRegex } from "rregex"
|
|
966
|
+
*
|
|
967
|
+
* const re = new RRegex("[^01]+")
|
|
838
968
|
* expect(re.replace("1078910", "").toBe("1010")
|
|
839
969
|
* ```
|
|
840
970
|
*
|
|
@@ -842,7 +972,9 @@ export class RRegex {
|
|
|
842
972
|
* groups:
|
|
843
973
|
*
|
|
844
974
|
* ```typescript
|
|
845
|
-
*
|
|
975
|
+
* import { RRegex } from "rregex"
|
|
976
|
+
*
|
|
977
|
+
* const re = new RRegex("(?P<last>[^,\\s]+),\\s+(?P<first>\\S+)")
|
|
846
978
|
* const result = re.replace("Springsteen, Bruce", "$first $last")
|
|
847
979
|
* expect(result).toBe("Bruce Springsteen")
|
|
848
980
|
* ```
|
|
@@ -856,7 +988,9 @@ export class RRegex {
|
|
|
856
988
|
* underscore:
|
|
857
989
|
*
|
|
858
990
|
* ```typescript
|
|
859
|
-
*
|
|
991
|
+
* import { RRegex } from "rregex"
|
|
992
|
+
*
|
|
993
|
+
* const re = new RRegex("(?P<first>\\w+)\\s+(?P<second>\\w+)")
|
|
860
994
|
* const result = re.replace("deep fried", "${first}_$second")
|
|
861
995
|
* expect(result).toBe("deep_fried")
|
|
862
996
|
* ```
|
|
@@ -913,7 +1047,9 @@ export class RRegex {
|
|
|
913
1047
|
* To split a string delimited by arbitrary amounts of spaces or tabs:
|
|
914
1048
|
*
|
|
915
1049
|
* ```typescript
|
|
916
|
-
*
|
|
1050
|
+
* import { RRegex } from "rregex"
|
|
1051
|
+
*
|
|
1052
|
+
* const re = new RRegex(r"[ \\t]+")
|
|
917
1053
|
* const fields = re.split("a b \t c\td e")
|
|
918
1054
|
* expect(fields).toEqual(["a", "b", "c", "d", "e"])
|
|
919
1055
|
* ```
|
|
@@ -937,7 +1073,9 @@ export class RRegex {
|
|
|
937
1073
|
* Get the first two words in some text:
|
|
938
1074
|
*
|
|
939
1075
|
* ```typescript
|
|
940
|
-
*
|
|
1076
|
+
* import { RRegex } from "rregex"
|
|
1077
|
+
*
|
|
1078
|
+
* const re = new RRegex(r"\\W+")
|
|
941
1079
|
* const fields = re.splitn("Hey! How are you?", 3)
|
|
942
1080
|
* expect(fields).toEqual(["Hey", "How", "are you?"])
|
|
943
1081
|
* ```
|
|
@@ -969,8 +1107,10 @@ export class RRegex {
|
|
|
969
1107
|
* `a`.
|
|
970
1108
|
*
|
|
971
1109
|
* ```typescript
|
|
1110
|
+
* import { RRegex } from "rregex"
|
|
1111
|
+
*
|
|
972
1112
|
* const text = "aaaaa"
|
|
973
|
-
* const = new
|
|
1113
|
+
* const = new RRegex("a+").shortest_match(text)
|
|
974
1114
|
* expect(pos).toBe(1)
|
|
975
1115
|
* ```
|
|
976
1116
|
*
|
|
@@ -1029,6 +1169,8 @@ export class RRegexSet {
|
|
|
1029
1169
|
* Create a new regex set from an iterator of strings:
|
|
1030
1170
|
*
|
|
1031
1171
|
* ```typescript
|
|
1172
|
+
* import { RegexSet } from "rregex"
|
|
1173
|
+
*
|
|
1032
1174
|
* const set = new RegexSet(["\\w+", "\\d+"])
|
|
1033
1175
|
* expect(set.is_match("foo")).toBe(true)
|
|
1034
1176
|
* ```
|
|
@@ -1055,6 +1197,8 @@ export class RRegexSet {
|
|
|
1055
1197
|
* Tests whether a set matches some text:
|
|
1056
1198
|
*
|
|
1057
1199
|
* ```typescript
|
|
1200
|
+
* import { RegexSet } from "rregex"
|
|
1201
|
+
*
|
|
1058
1202
|
* const set = new RegexSet(["\\w+", "\\d+"])
|
|
1059
1203
|
* expect(set.is_match("foo")).toBe(true)
|
|
1060
1204
|
* expect(!set.is_match("☃")).toBe(false)
|
|
@@ -1084,6 +1228,8 @@ export class RRegexSet {
|
|
|
1084
1228
|
* Tests which regular expressions match the given text:
|
|
1085
1229
|
*
|
|
1086
1230
|
* ```typescript
|
|
1231
|
+
* import { RegexSet } from "rregex"
|
|
1232
|
+
*
|
|
1087
1233
|
* const set = new RegexSet([
|
|
1088
1234
|
* "\\w+",
|
|
1089
1235
|
* "\\d+",
|
|
@@ -1112,6 +1258,6 @@ export const metadata: {
|
|
|
1112
1258
|
],
|
|
1113
1259
|
"homepage": "http://rregex.dev",
|
|
1114
1260
|
"repository": "https://github.com/2fd/rregex",
|
|
1115
|
-
"regex": "1.10.
|
|
1261
|
+
"regex": "1.10.3",
|
|
1116
1262
|
"regex-syntax": "0.8.2"
|
|
1117
1263
|
}
|