rregex 1.10.1 → 1.10.2
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 +163 -17
- package/lib/browser.js +80 -11
- package/lib/commonjs.d.ts +163 -17
- package/lib/commonjs.js +80 -11
- package/lib/module.d.ts +163 -17
- package/lib/module.js +80 -11
- package/lib/types.d.ts +163 -17
- package/package.json +1 -1
package/lib/browser.js
CHANGED
|
@@ -178,6 +178,43 @@ function handleError(f, args) {
|
|
|
178
178
|
}
|
|
179
179
|
/**
|
|
180
180
|
* A compiled regular expression for matching Unicode strings.
|
|
181
|
+
*
|
|
182
|
+
* A `RRegex` can be used to search haystacks, split haystacks into substrings
|
|
183
|
+
* or replace substrings in a haystack with a different substring. All
|
|
184
|
+
* searching is done with an implicit `(?s:.)*?` at the beginning and end of
|
|
185
|
+
* an pattern. To force an expression to match the whole string (or a prefix
|
|
186
|
+
* or a suffix), you must use an anchor like `^` or `$` (or `\A` and `\z`).
|
|
187
|
+
*
|
|
188
|
+
* While this library will handle Unicode strings (whether in the regular
|
|
189
|
+
* expression or in the haystack), all positions returned are **byte
|
|
190
|
+
* offsets**. Every byte offset is guaranteed to be at a Unicode code point
|
|
191
|
+
* boundary. That is, all offsets returned by the `RRegex` API are guaranteed
|
|
192
|
+
* to be ranges that can slice an `Uint8Array` created with a TextEncoder.
|
|
193
|
+
*
|
|
194
|
+
* # Example: slicing over UTF strings
|
|
195
|
+
*
|
|
196
|
+
* ```typescript
|
|
197
|
+
* import { RRegex } from "rregex"
|
|
198
|
+
*
|
|
199
|
+
* const re = new RRegex("ä")
|
|
200
|
+
* const m = re.find("äöü") // { start: 0, end: 2 }
|
|
201
|
+
*
|
|
202
|
+
* const buff = new TextEncoder().encode("äöü")
|
|
203
|
+
* const slice = buff.slice(m.start, m.end)
|
|
204
|
+
* expect(new TextDecoder().decode(slice)).toBe("ä")
|
|
205
|
+
* ```
|
|
206
|
+
*
|
|
207
|
+
* # Example: find the offsets of a US phone number:
|
|
208
|
+
*
|
|
209
|
+
* ```typescript
|
|
210
|
+
* import { RRegex } from "rregex"
|
|
211
|
+
*
|
|
212
|
+
* const re = new RRegex("[0-9]{3}-[0-9]{3}-[0-9]{4}");
|
|
213
|
+
* const m = re.find("phone: 111-222-3333");
|
|
214
|
+
* expect(m.start).toBe(7);
|
|
215
|
+
* expect(m.end).toBe(19);
|
|
216
|
+
* ```
|
|
217
|
+
*
|
|
181
218
|
* @see https://docs.rs/regex/latest/regex/
|
|
182
219
|
*/
|
|
183
220
|
export class RRegex {
|
|
@@ -232,6 +269,8 @@ export class RRegex {
|
|
|
232
269
|
* Unicode word characters:
|
|
233
270
|
*
|
|
234
271
|
* ```typescript
|
|
272
|
+
* import { RRegex } from "rregex"
|
|
273
|
+
*
|
|
235
274
|
* const text = "I categorically deny having triskaidekaphobia."
|
|
236
275
|
* expect(new RRegex("\\b\\w{13}\\b").is_match(text)).toBe(true)
|
|
237
276
|
* ```
|
|
@@ -277,8 +316,10 @@ export class RRegex {
|
|
|
277
316
|
* Unicode word characters:
|
|
278
317
|
*
|
|
279
318
|
* ```typescript
|
|
319
|
+
* import { RRegex } from "rregex"
|
|
320
|
+
*
|
|
280
321
|
* const text = "I categorically deny having triskaidekaphobia."
|
|
281
|
-
* const m = new
|
|
322
|
+
* const m = new RRegex("\\b\\w{13}\\b").find(text)
|
|
282
323
|
* expect(m.start).toBe(2)
|
|
283
324
|
* expect(m.end).toBe(15)
|
|
284
325
|
* ```
|
|
@@ -344,8 +385,10 @@ export class RRegex {
|
|
|
344
385
|
* word characters:
|
|
345
386
|
*
|
|
346
387
|
* ```typescript
|
|
388
|
+
* import { RRegex } from "rregex"
|
|
389
|
+
*
|
|
347
390
|
* const text = "Retroactively relinquishing remunerations is reprehensible."
|
|
348
|
-
* const matches = new
|
|
391
|
+
* const matches = new RRegex("\\b\\w{13}\\b").findAll(text)
|
|
349
392
|
* ```
|
|
350
393
|
*
|
|
351
394
|
* @see https://docs.rs/regex/latest/regex/struct.Regex.html#method.find_iter
|
|
@@ -404,7 +447,9 @@ export class RRegex {
|
|
|
404
447
|
* year separately.
|
|
405
448
|
*
|
|
406
449
|
* ```typescript
|
|
407
|
-
*
|
|
450
|
+
* import { RRegex } from "rregex"
|
|
451
|
+
*
|
|
452
|
+
* const re = new RRegex("'([^']+)'\\s+\\((\\d{4})\)")
|
|
408
453
|
* const text = "Not my favorite movie: 'Citizen Kane' (1941)."
|
|
409
454
|
* const caps = re.captures(text)
|
|
410
455
|
* expect(caps.get[1].value).toBe("Citizen Kane")
|
|
@@ -418,7 +463,9 @@ export class RRegex {
|
|
|
418
463
|
* We can make this example a bit clearer by using *named* capture groups:
|
|
419
464
|
*
|
|
420
465
|
* ```typescript
|
|
421
|
-
*
|
|
466
|
+
* import { RRegex } from "rregex"
|
|
467
|
+
*
|
|
468
|
+
* const re = new RRegex("'(?P<title>[^']+)'\\s+\\((?P<year>\\d{4})\)")
|
|
422
469
|
* const text = "Not my favorite movie: 'Citizen Kane' (1941)."
|
|
423
470
|
* const caps = re.captures(text)
|
|
424
471
|
* expect(caps.name["title"].value).toBe("Citizen Kane")
|
|
@@ -458,7 +505,9 @@ export class RRegex {
|
|
|
458
505
|
* some text, where the movie is formatted like "'Title' (xxxx)":
|
|
459
506
|
*
|
|
460
507
|
* ```typescript
|
|
461
|
-
*
|
|
508
|
+
* import { RRegex } from "rregex"
|
|
509
|
+
*
|
|
510
|
+
* const re = new RRegex("'(?P<title>[^']+)'\\s+\\((?P<year>\\d{4})\\)")
|
|
462
511
|
* const text = "'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931)."
|
|
463
512
|
* for caps of re.captures_iter(text) {
|
|
464
513
|
* console.log(
|
|
@@ -505,6 +554,8 @@ export class RRegex {
|
|
|
505
554
|
* ## Example
|
|
506
555
|
*
|
|
507
556
|
* ```typescript
|
|
557
|
+
* import { RRegex } from "rregex"
|
|
558
|
+
*
|
|
508
559
|
* const re1 = new RRegex("(?P<y>\\d{4})-(?P<m>\\d{2})-(?P<d>\\d{2})")
|
|
509
560
|
* expect(re1.capturesLength()).toEqual(4)
|
|
510
561
|
*
|
|
@@ -561,7 +612,9 @@ export class RRegex {
|
|
|
561
612
|
* In typical usage, this can just be a normal string:
|
|
562
613
|
*
|
|
563
614
|
* ```typescript
|
|
564
|
-
*
|
|
615
|
+
* import { RRegex } from "rregex"
|
|
616
|
+
*
|
|
617
|
+
* const re = new RRegex("[^01]+")
|
|
565
618
|
* expect(re.replace("1078910", "").toBe("1010")
|
|
566
619
|
* ```
|
|
567
620
|
*
|
|
@@ -569,7 +622,9 @@ export class RRegex {
|
|
|
569
622
|
* groups:
|
|
570
623
|
*
|
|
571
624
|
* ```typescript
|
|
572
|
-
*
|
|
625
|
+
* import { RRegex } from "rregex"
|
|
626
|
+
*
|
|
627
|
+
* const re = new RRegex("(?P<last>[^,\\s]+),\\s+(?P<first>\\S+)")
|
|
573
628
|
* const result = re.replace("Springsteen, Bruce", "$first $last")
|
|
574
629
|
* expect(result).toBe("Bruce Springsteen")
|
|
575
630
|
* ```
|
|
@@ -583,7 +638,9 @@ export class RRegex {
|
|
|
583
638
|
* underscore:
|
|
584
639
|
*
|
|
585
640
|
* ```typescript
|
|
586
|
-
*
|
|
641
|
+
* import { RRegex } from "rregex"
|
|
642
|
+
*
|
|
643
|
+
* const re = new RRegex("(?P<first>\\w+)\\s+(?P<second>\\w+)")
|
|
587
644
|
* const result = re.replace("deep fried", "${first}_$second")
|
|
588
645
|
* expect(result).toBe("deep_fried")
|
|
589
646
|
* ```
|
|
@@ -697,7 +754,9 @@ export class RRegex {
|
|
|
697
754
|
* To split a string delimited by arbitrary amounts of spaces or tabs:
|
|
698
755
|
*
|
|
699
756
|
* ```typescript
|
|
700
|
-
*
|
|
757
|
+
* import { RRegex } from "rregex"
|
|
758
|
+
*
|
|
759
|
+
* const re = new RRegex(r"[ \\t]+")
|
|
701
760
|
* const fields = re.split("a b \t c\td e")
|
|
702
761
|
* expect(fields).toEqual(["a", "b", "c", "d", "e"])
|
|
703
762
|
* ```
|
|
@@ -735,7 +794,9 @@ export class RRegex {
|
|
|
735
794
|
* Get the first two words in some text:
|
|
736
795
|
*
|
|
737
796
|
* ```typescript
|
|
738
|
-
*
|
|
797
|
+
* import { RRegex } from "rregex"
|
|
798
|
+
*
|
|
799
|
+
* const re = new RRegex(r"\\W+")
|
|
739
800
|
* const fields = re.splitn("Hey! How are you?", 3)
|
|
740
801
|
* expect(fields).toEqual(["Hey", "How", "are you?"])
|
|
741
802
|
* ```
|
|
@@ -781,8 +842,10 @@ export class RRegex {
|
|
|
781
842
|
* `a`.
|
|
782
843
|
*
|
|
783
844
|
* ```typescript
|
|
845
|
+
* import { RRegex } from "rregex"
|
|
846
|
+
*
|
|
784
847
|
* const text = "aaaaa"
|
|
785
|
-
* const = new
|
|
848
|
+
* const = new RRegex("a+").shortest_match(text)
|
|
786
849
|
* expect(pos).toBe(1)
|
|
787
850
|
* ```
|
|
788
851
|
*
|
|
@@ -905,6 +968,8 @@ export class RRegexSet {
|
|
|
905
968
|
* Create a new regex set from an iterator of strings:
|
|
906
969
|
*
|
|
907
970
|
* ```typescript
|
|
971
|
+
* import { RegexSet } from "rregex"
|
|
972
|
+
*
|
|
908
973
|
* const set = new RegexSet(["\\w+", "\\d+"])
|
|
909
974
|
* expect(set.is_match("foo")).toBe(true)
|
|
910
975
|
* ```
|
|
@@ -947,6 +1012,8 @@ export class RRegexSet {
|
|
|
947
1012
|
* Tests whether a set matches some text:
|
|
948
1013
|
*
|
|
949
1014
|
* ```typescript
|
|
1015
|
+
* import { RegexSet } from "rregex"
|
|
1016
|
+
*
|
|
950
1017
|
* const set = new RegexSet(["\\w+", "\\d+"])
|
|
951
1018
|
* expect(set.is_match("foo")).toBe(true)
|
|
952
1019
|
* expect(!set.is_match("☃")).toBe(false)
|
|
@@ -981,6 +1048,8 @@ export class RRegexSet {
|
|
|
981
1048
|
* Tests which regular expressions match the given text:
|
|
982
1049
|
*
|
|
983
1050
|
* ```typescript
|
|
1051
|
+
* import { RegexSet } from "rregex"
|
|
1052
|
+
*
|
|
984
1053
|
* const set = new RegexSet([
|
|
985
1054
|
* "\\w+",
|
|
986
1055
|
* "\\d+",
|
package/lib/commonjs.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+",
|