@nevware21/ts-utils
    Preparing search index...

    Function polyStrMatchAll

    • Polyfill implementation of String.prototype.matchAll() that returns an iterator of all results matching a string against a regular expression, including capturing groups.

      Matches the behaviour of the native String.prototype.matchAll() method:

      • A RegExp matcher must carry the global (g) flag; passing a non-global RegExp throws a TypeError.
      • A non-RegExp matcher is passed to new RegExp(matcher, "g"), mirroring native behavior.
      • If matcher exposes a [Symbol.matchAll] method, that method is called with the coerced string value, mirroring the native delegation behaviour.
      • Zero-length matches advance lastIndex using native-style AdvanceStringIndex behavior, including code-point advancement for unicode regexes.

      Parameters

      • value: string

        The string value to search.

      • matcher: string | RegExp

        A RegExp with the global flag set, a matcher value used to create a global RegExp, or any object with a [Symbol.matchAll] method.

      Returns IterableIterator<RegExpExecArray>

      An IterableIterator<RegExpExecArray> where each RegExpExecArray contains:

      • [0] — the full matched substring
      • [1..n] — captured groups (or undefined for unmatched optional groups)
      • .index — zero-based index of the match in value
      • .input — the original string
      • .groups — named capture groups object, or undefined if no named captures

      0.14.0

      TypeError if matcher is a RegExp without the global (g) flag.

      TypeError if value is null or undefined.

      // Basic global regex
      const matches = [...polyStrMatchAll("test1 test2", /test(\d)/g)];
      matches.length; // 2
      matches[0][0]; // "test1"
      matches[0][1]; // "1"
      matches[0].index; // 0
      // String matcher (native RegExp(pattern, "g") semantics)
      const hits = [...polyStrMatchAll("banana", "an")];
      hits.length; // 2
      hits[0].index; // 1
      hits[1].index; // 3
      // Named capture groups
      const re = /(?<word>\w+)/g;
      const words = [...polyStrMatchAll("hello world", re)];
      words[0].groups; // { word: "hello" }
      words[1].groups; // { word: "world" }