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/module.js
CHANGED
|
@@ -4,6 +4,26 @@ export function __wbg_set_wasm(val) {
|
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
|
|
7
|
+
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
|
|
8
|
+
|
|
9
|
+
let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
10
|
+
|
|
11
|
+
cachedTextDecoder.decode();
|
|
12
|
+
|
|
13
|
+
let cachedUint8Memory0 = null;
|
|
14
|
+
|
|
15
|
+
function getUint8Memory0() {
|
|
16
|
+
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
|
|
17
|
+
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
|
|
18
|
+
}
|
|
19
|
+
return cachedUint8Memory0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getStringFromWasm0(ptr, len) {
|
|
23
|
+
ptr = ptr >>> 0;
|
|
24
|
+
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
|
|
25
|
+
}
|
|
26
|
+
|
|
7
27
|
const heap = new Array(128).fill(undefined);
|
|
8
28
|
|
|
9
29
|
heap.push(undefined, null, true, false);
|
|
@@ -33,26 +53,6 @@ function takeObject(idx) {
|
|
|
33
53
|
return ret;
|
|
34
54
|
}
|
|
35
55
|
|
|
36
|
-
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
|
|
37
|
-
|
|
38
|
-
let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
39
|
-
|
|
40
|
-
cachedTextDecoder.decode();
|
|
41
|
-
|
|
42
|
-
let cachedUint8Memory0 = null;
|
|
43
|
-
|
|
44
|
-
function getUint8Memory0() {
|
|
45
|
-
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
|
|
46
|
-
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
|
|
47
|
-
}
|
|
48
|
-
return cachedUint8Memory0;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function getStringFromWasm0(ptr, len) {
|
|
52
|
-
ptr = ptr >>> 0;
|
|
53
|
-
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
|
|
54
|
-
}
|
|
55
|
-
|
|
56
56
|
let WASM_VECTOR_LEN = 0;
|
|
57
57
|
|
|
58
58
|
const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder;
|
|
@@ -186,6 +186,43 @@ function handleError(f, args) {
|
|
|
186
186
|
}
|
|
187
187
|
/**
|
|
188
188
|
* A compiled regular expression for matching Unicode strings.
|
|
189
|
+
*
|
|
190
|
+
* A `RRegex` can be used to search haystacks, split haystacks into substrings
|
|
191
|
+
* or replace substrings in a haystack with a different substring. All
|
|
192
|
+
* searching is done with an implicit `(?s:.)*?` at the beginning and end of
|
|
193
|
+
* an pattern. To force an expression to match the whole string (or a prefix
|
|
194
|
+
* or a suffix), you must use an anchor like `^` or `$` (or `\A` and `\z`).
|
|
195
|
+
*
|
|
196
|
+
* While this library will handle Unicode strings (whether in the regular
|
|
197
|
+
* expression or in the haystack), all positions returned are **byte
|
|
198
|
+
* offsets**. Every byte offset is guaranteed to be at a Unicode code point
|
|
199
|
+
* boundary. That is, all offsets returned by the `RRegex` API are guaranteed
|
|
200
|
+
* to be ranges that can slice an `Uint8Array` created with a TextEncoder.
|
|
201
|
+
*
|
|
202
|
+
* # Example: slicing over UTF strings
|
|
203
|
+
*
|
|
204
|
+
* ```typescript
|
|
205
|
+
* import { RRegex } from "rregex"
|
|
206
|
+
*
|
|
207
|
+
* const re = new RRegex("ä")
|
|
208
|
+
* const m = re.find("äöü") // { start: 0, end: 2 }
|
|
209
|
+
*
|
|
210
|
+
* const buff = new TextEncoder().encode("äöü")
|
|
211
|
+
* const slice = buff.slice(m.start, m.end)
|
|
212
|
+
* expect(new TextDecoder().decode(slice)).toBe("ä")
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* # Example: find the offsets of a US phone number:
|
|
216
|
+
*
|
|
217
|
+
* ```typescript
|
|
218
|
+
* import { RRegex } from "rregex"
|
|
219
|
+
*
|
|
220
|
+
* const re = new RRegex("[0-9]{3}-[0-9]{3}-[0-9]{4}");
|
|
221
|
+
* const m = re.find("phone: 111-222-3333");
|
|
222
|
+
* expect(m.start).toBe(7);
|
|
223
|
+
* expect(m.end).toBe(19);
|
|
224
|
+
* ```
|
|
225
|
+
*
|
|
189
226
|
* @see https://docs.rs/regex/latest/regex/
|
|
190
227
|
*/
|
|
191
228
|
export class RRegex {
|
|
@@ -240,6 +277,8 @@ export class RRegex {
|
|
|
240
277
|
* Unicode word characters:
|
|
241
278
|
*
|
|
242
279
|
* ```typescript
|
|
280
|
+
* import { RRegex } from "rregex"
|
|
281
|
+
*
|
|
243
282
|
* const text = "I categorically deny having triskaidekaphobia."
|
|
244
283
|
* expect(new RRegex("\\b\\w{13}\\b").is_match(text)).toBe(true)
|
|
245
284
|
* ```
|
|
@@ -285,8 +324,10 @@ export class RRegex {
|
|
|
285
324
|
* Unicode word characters:
|
|
286
325
|
*
|
|
287
326
|
* ```typescript
|
|
327
|
+
* import { RRegex } from "rregex"
|
|
328
|
+
*
|
|
288
329
|
* const text = "I categorically deny having triskaidekaphobia."
|
|
289
|
-
* const m = new
|
|
330
|
+
* const m = new RRegex("\\b\\w{13}\\b").find(text)
|
|
290
331
|
* expect(m.start).toBe(2)
|
|
291
332
|
* expect(m.end).toBe(15)
|
|
292
333
|
* ```
|
|
@@ -352,8 +393,10 @@ export class RRegex {
|
|
|
352
393
|
* word characters:
|
|
353
394
|
*
|
|
354
395
|
* ```typescript
|
|
396
|
+
* import { RRegex } from "rregex"
|
|
397
|
+
*
|
|
355
398
|
* const text = "Retroactively relinquishing remunerations is reprehensible."
|
|
356
|
-
* const matches = new
|
|
399
|
+
* const matches = new RRegex("\\b\\w{13}\\b").findAll(text)
|
|
357
400
|
* ```
|
|
358
401
|
*
|
|
359
402
|
* @see https://docs.rs/regex/latest/regex/struct.Regex.html#method.find_iter
|
|
@@ -412,7 +455,9 @@ export class RRegex {
|
|
|
412
455
|
* year separately.
|
|
413
456
|
*
|
|
414
457
|
* ```typescript
|
|
415
|
-
*
|
|
458
|
+
* import { RRegex } from "rregex"
|
|
459
|
+
*
|
|
460
|
+
* const re = new RRegex("'([^']+)'\\s+\\((\\d{4})\)")
|
|
416
461
|
* const text = "Not my favorite movie: 'Citizen Kane' (1941)."
|
|
417
462
|
* const caps = re.captures(text)
|
|
418
463
|
* expect(caps.get[1].value).toBe("Citizen Kane")
|
|
@@ -426,7 +471,9 @@ export class RRegex {
|
|
|
426
471
|
* We can make this example a bit clearer by using *named* capture groups:
|
|
427
472
|
*
|
|
428
473
|
* ```typescript
|
|
429
|
-
*
|
|
474
|
+
* import { RRegex } from "rregex"
|
|
475
|
+
*
|
|
476
|
+
* const re = new RRegex("'(?P<title>[^']+)'\\s+\\((?P<year>\\d{4})\)")
|
|
430
477
|
* const text = "Not my favorite movie: 'Citizen Kane' (1941)."
|
|
431
478
|
* const caps = re.captures(text)
|
|
432
479
|
* expect(caps.name["title"].value).toBe("Citizen Kane")
|
|
@@ -466,7 +513,9 @@ export class RRegex {
|
|
|
466
513
|
* some text, where the movie is formatted like "'Title' (xxxx)":
|
|
467
514
|
*
|
|
468
515
|
* ```typescript
|
|
469
|
-
*
|
|
516
|
+
* import { RRegex } from "rregex"
|
|
517
|
+
*
|
|
518
|
+
* const re = new RRegex("'(?P<title>[^']+)'\\s+\\((?P<year>\\d{4})\\)")
|
|
470
519
|
* const text = "'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931)."
|
|
471
520
|
* for caps of re.captures_iter(text) {
|
|
472
521
|
* console.log(
|
|
@@ -513,6 +562,8 @@ export class RRegex {
|
|
|
513
562
|
* ## Example
|
|
514
563
|
*
|
|
515
564
|
* ```typescript
|
|
565
|
+
* import { RRegex } from "rregex"
|
|
566
|
+
*
|
|
516
567
|
* const re1 = new RRegex("(?P<y>\\d{4})-(?P<m>\\d{2})-(?P<d>\\d{2})")
|
|
517
568
|
* expect(re1.capturesLength()).toEqual(4)
|
|
518
569
|
*
|
|
@@ -569,7 +620,9 @@ export class RRegex {
|
|
|
569
620
|
* In typical usage, this can just be a normal string:
|
|
570
621
|
*
|
|
571
622
|
* ```typescript
|
|
572
|
-
*
|
|
623
|
+
* import { RRegex } from "rregex"
|
|
624
|
+
*
|
|
625
|
+
* const re = new RRegex("[^01]+")
|
|
573
626
|
* expect(re.replace("1078910", "").toBe("1010")
|
|
574
627
|
* ```
|
|
575
628
|
*
|
|
@@ -577,7 +630,9 @@ export class RRegex {
|
|
|
577
630
|
* groups:
|
|
578
631
|
*
|
|
579
632
|
* ```typescript
|
|
580
|
-
*
|
|
633
|
+
* import { RRegex } from "rregex"
|
|
634
|
+
*
|
|
635
|
+
* const re = new RRegex("(?P<last>[^,\\s]+),\\s+(?P<first>\\S+)")
|
|
581
636
|
* const result = re.replace("Springsteen, Bruce", "$first $last")
|
|
582
637
|
* expect(result).toBe("Bruce Springsteen")
|
|
583
638
|
* ```
|
|
@@ -591,7 +646,9 @@ export class RRegex {
|
|
|
591
646
|
* underscore:
|
|
592
647
|
*
|
|
593
648
|
* ```typescript
|
|
594
|
-
*
|
|
649
|
+
* import { RRegex } from "rregex"
|
|
650
|
+
*
|
|
651
|
+
* const re = new RRegex("(?P<first>\\w+)\\s+(?P<second>\\w+)")
|
|
595
652
|
* const result = re.replace("deep fried", "${first}_$second")
|
|
596
653
|
* expect(result).toBe("deep_fried")
|
|
597
654
|
* ```
|
|
@@ -705,7 +762,9 @@ export class RRegex {
|
|
|
705
762
|
* To split a string delimited by arbitrary amounts of spaces or tabs:
|
|
706
763
|
*
|
|
707
764
|
* ```typescript
|
|
708
|
-
*
|
|
765
|
+
* import { RRegex } from "rregex"
|
|
766
|
+
*
|
|
767
|
+
* const re = new RRegex(r"[ \\t]+")
|
|
709
768
|
* const fields = re.split("a b \t c\td e")
|
|
710
769
|
* expect(fields).toEqual(["a", "b", "c", "d", "e"])
|
|
711
770
|
* ```
|
|
@@ -743,7 +802,9 @@ export class RRegex {
|
|
|
743
802
|
* Get the first two words in some text:
|
|
744
803
|
*
|
|
745
804
|
* ```typescript
|
|
746
|
-
*
|
|
805
|
+
* import { RRegex } from "rregex"
|
|
806
|
+
*
|
|
807
|
+
* const re = new RRegex(r"\\W+")
|
|
747
808
|
* const fields = re.splitn("Hey! How are you?", 3)
|
|
748
809
|
* expect(fields).toEqual(["Hey", "How", "are you?"])
|
|
749
810
|
* ```
|
|
@@ -789,8 +850,10 @@ export class RRegex {
|
|
|
789
850
|
* `a`.
|
|
790
851
|
*
|
|
791
852
|
* ```typescript
|
|
853
|
+
* import { RRegex } from "rregex"
|
|
854
|
+
*
|
|
792
855
|
* const text = "aaaaa"
|
|
793
|
-
* const = new
|
|
856
|
+
* const = new RRegex("a+").shortest_match(text)
|
|
794
857
|
* expect(pos).toBe(1)
|
|
795
858
|
* ```
|
|
796
859
|
*
|
|
@@ -913,6 +976,8 @@ export class RRegexSet {
|
|
|
913
976
|
* Create a new regex set from an iterator of strings:
|
|
914
977
|
*
|
|
915
978
|
* ```typescript
|
|
979
|
+
* import { RegexSet } from "rregex"
|
|
980
|
+
*
|
|
916
981
|
* const set = new RegexSet(["\\w+", "\\d+"])
|
|
917
982
|
* expect(set.is_match("foo")).toBe(true)
|
|
918
983
|
* ```
|
|
@@ -955,6 +1020,8 @@ export class RRegexSet {
|
|
|
955
1020
|
* Tests whether a set matches some text:
|
|
956
1021
|
*
|
|
957
1022
|
* ```typescript
|
|
1023
|
+
* import { RegexSet } from "rregex"
|
|
1024
|
+
*
|
|
958
1025
|
* const set = new RegexSet(["\\w+", "\\d+"])
|
|
959
1026
|
* expect(set.is_match("foo")).toBe(true)
|
|
960
1027
|
* expect(!set.is_match("☃")).toBe(false)
|
|
@@ -989,6 +1056,8 @@ export class RRegexSet {
|
|
|
989
1056
|
* Tests which regular expressions match the given text:
|
|
990
1057
|
*
|
|
991
1058
|
* ```typescript
|
|
1059
|
+
* import { RegexSet } from "rregex"
|
|
1060
|
+
*
|
|
992
1061
|
* const set = new RegexSet([
|
|
993
1062
|
* "\\w+",
|
|
994
1063
|
* "\\d+",
|
|
@@ -1023,8 +1092,8 @@ export class RRegexSet {
|
|
|
1023
1092
|
}
|
|
1024
1093
|
}
|
|
1025
1094
|
|
|
1026
|
-
export function
|
|
1027
|
-
const ret = arg0;
|
|
1095
|
+
export function __wbindgen_string_new(arg0, arg1) {
|
|
1096
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
1028
1097
|
return addHeapObject(ret);
|
|
1029
1098
|
};
|
|
1030
1099
|
|
|
@@ -1032,8 +1101,8 @@ export function __wbindgen_object_drop_ref(arg0) {
|
|
|
1032
1101
|
takeObject(arg0);
|
|
1033
1102
|
};
|
|
1034
1103
|
|
|
1035
|
-
export function
|
|
1036
|
-
const ret =
|
|
1104
|
+
export function __wbindgen_number_new(arg0) {
|
|
1105
|
+
const ret = arg0;
|
|
1037
1106
|
return addHeapObject(ret);
|
|
1038
1107
|
};
|
|
1039
1108
|
|
|
@@ -1065,61 +1134,61 @@ export function __wbg_set_9182712abebf82ef(arg0, arg1, arg2) {
|
|
|
1065
1134
|
getObject(arg0)[takeObject(arg1)] = takeObject(arg2);
|
|
1066
1135
|
};
|
|
1067
1136
|
|
|
1068
|
-
export function
|
|
1137
|
+
export function __wbg_get_c43534c00f382c8a(arg0, arg1) {
|
|
1069
1138
|
const ret = getObject(arg0)[arg1 >>> 0];
|
|
1070
1139
|
return addHeapObject(ret);
|
|
1071
1140
|
};
|
|
1072
1141
|
|
|
1073
|
-
export function
|
|
1142
|
+
export function __wbg_length_d99b680fd68bf71b(arg0) {
|
|
1074
1143
|
const ret = getObject(arg0).length;
|
|
1075
1144
|
return ret;
|
|
1076
1145
|
};
|
|
1077
1146
|
|
|
1078
|
-
export function
|
|
1147
|
+
export function __wbg_new_34c624469fb1d4fd() {
|
|
1079
1148
|
const ret = new Array();
|
|
1080
1149
|
return addHeapObject(ret);
|
|
1081
1150
|
};
|
|
1082
1151
|
|
|
1083
|
-
export function
|
|
1152
|
+
export function __wbg_new_87d841e70661f6e9() {
|
|
1084
1153
|
const ret = new Object();
|
|
1085
1154
|
return addHeapObject(ret);
|
|
1086
1155
|
};
|
|
1087
1156
|
|
|
1088
|
-
export function
|
|
1157
|
+
export function __wbg_set_379b27f1d5f1bf9c(arg0, arg1, arg2) {
|
|
1089
1158
|
getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
|
|
1090
1159
|
};
|
|
1091
1160
|
|
|
1092
|
-
export function
|
|
1161
|
+
export function __wbg_from_a663e01d8dab8e44(arg0) {
|
|
1093
1162
|
const ret = Array.from(getObject(arg0));
|
|
1094
1163
|
return addHeapObject(ret);
|
|
1095
1164
|
};
|
|
1096
1165
|
|
|
1097
|
-
export function
|
|
1166
|
+
export function __wbg_push_906164999551d793(arg0, arg1) {
|
|
1098
1167
|
const ret = getObject(arg0).push(getObject(arg1));
|
|
1099
1168
|
return ret;
|
|
1100
1169
|
};
|
|
1101
1170
|
|
|
1102
|
-
export function
|
|
1171
|
+
export function __wbg_fromCodePoint_33fc13cdbfe3bca7() { return handleError(function (arg0) {
|
|
1103
1172
|
const ret = String.fromCodePoint(arg0 >>> 0);
|
|
1104
1173
|
return addHeapObject(ret);
|
|
1105
1174
|
}, arguments) };
|
|
1106
1175
|
|
|
1107
|
-
export function
|
|
1176
|
+
export function __wbg_buffer_5d1b598a01b41a42(arg0) {
|
|
1108
1177
|
const ret = getObject(arg0).buffer;
|
|
1109
1178
|
return addHeapObject(ret);
|
|
1110
1179
|
};
|
|
1111
1180
|
|
|
1112
|
-
export function
|
|
1181
|
+
export function __wbg_newwithbyteoffsetandlength_d695c7957788f922(arg0, arg1, arg2) {
|
|
1113
1182
|
const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
|
|
1114
1183
|
return addHeapObject(ret);
|
|
1115
1184
|
};
|
|
1116
1185
|
|
|
1117
|
-
export function
|
|
1186
|
+
export function __wbg_new_ace717933ad7117f(arg0) {
|
|
1118
1187
|
const ret = new Uint8Array(getObject(arg0));
|
|
1119
1188
|
return addHeapObject(ret);
|
|
1120
1189
|
};
|
|
1121
1190
|
|
|
1122
|
-
export function
|
|
1191
|
+
export function __wbg_set_37a50e901587b477() { return handleError(function (arg0, arg1, arg2) {
|
|
1123
1192
|
const ret = Reflect.set(getObject(arg0), getObject(arg1), getObject(arg2));
|
|
1124
1193
|
return ret;
|
|
1125
1194
|
}, arguments) };
|
package/lib/rregex.wasm
CHANGED
|
Binary file
|
package/lib/types.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+",
|
|
@@ -1169,6 +1315,6 @@ export const metadata: {
|
|
|
1169
1315
|
],
|
|
1170
1316
|
"homepage": "http://rregex.dev",
|
|
1171
1317
|
"repository": "https://github.com/2fd/rregex",
|
|
1172
|
-
"regex": "1.10.
|
|
1318
|
+
"regex": "1.10.3",
|
|
1173
1319
|
"regex-syntax": "0.8.2"
|
|
1174
1320
|
}
|