re2js 2.7.1 → 2.8.1

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 CHANGED
@@ -513,7 +513,7 @@ RE2JS.compile('(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)')
513
513
  Note that the replacement string can include references to capturing groups from the pattern
514
514
 
515
515
  Parameters:
516
- - `replacement (String)`: The string that replaces the substrings found. Capture groups and special characters in the replacement string have special behavior. For example:
516
+ - `replacement (String | Function)`: The string that replaces the substrings found, or a function invoked to create the new substring. When passing a string, capture groups and special characters have special behavior. For example:
517
517
  - `$&` refers to the entire matched substring
518
518
  - `$1, $2, ...` refer to the corresponding capture groups in the pattern
519
519
  - `$$` inserts a literal `$`
@@ -556,7 +556,42 @@ RE2JS.compile('(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)')
556
556
  .replaceFirst('$10$20') // 'jb0nopqrstuvwxyz123'
557
557
  ```
558
558
 
559
- Function support second argument `javaMode`, which work in the same way, as for `replaceAll` function
559
+ Function support second argument `javaMode`, which work in the same way, as for `replaceAll` function.
560
+
561
+ #### Using a Replacer Function
562
+
563
+ For a more modern JavaScript developer experience, RE2JS supports passing a **replacer function** to `replaceAll()` and `replaceFirst()`, perfectly mirroring native `String.prototype.replace(regex, replacer)` behavior while taking advantage of the high-speed linear-time engine.
564
+
565
+ The replacer function is invoked for each match, and its return value is used as the replacement string. The function receives the following arguments:
566
+
567
+ 1. `match`: The matched substring.
568
+ 2. `p1, p2, ...`: The string found by a capture group (if any). Unmatched optional groups evaluate to `undefined`.
569
+ 3. `offset`: The offset of the matched substring within the whole string.
570
+ 4. `string`: The original input string (or byte array).
571
+ 5. `groups`: A dictionary object of named capture groups (if any exist in the pattern).
572
+
573
+ ```js
574
+ import { RE2JS } from 're2js'
575
+
576
+ // Example 1: Dynamic replacements
577
+ const re1 = RE2JS.compile('\\d+');
578
+ const m1 = re1.matcher('Numbers: 1, 2, 3');
579
+
580
+ m1.replaceAll((match) => String(Number(match) * 10));
581
+ // 'Numbers: 10, 20, 30'
582
+
583
+
584
+ // Example 2: Using named capture groups and function signature
585
+ const re2 = RE2JS.compile('(?P<first>\\w+) (?:(?P<middle>\\w+) )?(?P<last>\\w+)');
586
+ const m2 = re2.matcher('Hello World');
587
+
588
+ m2.replaceFirst((match, p1, p2, p3, offset, string, groups) => {
589
+ // 'middle' didn't match, so p2 and groups.middle will be undefined
590
+ return `${groups.last}, ${groups.first}`;
591
+ });
592
+ // 'World, Hello'
593
+
594
+ ```
560
595
 
561
596
  ### Safe Replacements
562
597