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

    Function strMatchAll

    • The strMatchAll() method returns an iterator of all results matching a string against a regular expression, including capturing groups. Unlike strMatch, it returns every match in the string (not just the first) and each result includes full RegExpExecArray details: matched substrings, capture groups, the match index, and the original input string.

      If matcher is a RegExp it must have the global (g) flag set; a non-global RegExp throws a TypeError. If matcher is not a RegExp, native behavior is followed by creating a global RegExp from the matcher value (for example, "." matches any character and undefined behaves as an empty pattern).

      If matcher is an object that implements [Symbol.matchAll], that method is called instead, allowing custom matcher objects to control the iteration.

      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 — all matches with capture groups
      const matches = [...strMatchAll("test1 test2 test3", /test(\d)/g)];
      matches.length; // 3
      matches[0][0]; // "test1"
      matches[0][1]; // "1"
      matches[0].index; // 0
      matches[1][0]; // "test2"
      matches[2][1]; // "3"
      // Named capture groups
      const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g;
      const dates = [...strMatchAll("2024-01-15 and 2025-06-30", re)];
      dates[0].groups; // { year: "2024", month: "01", day: "15" }
      dates[1].groups; // { year: "2025", month: "06", day: "30" }
      // String matcher — treated like native RegExp(pattern, "g")
      const hits = [...strMatchAll("banana", "an")];
      hits.length; // 2
      hits[0].index; // 1
      hits[1].index; // 3
      // Non-global RegExp throws TypeError
      strMatchAll("hello", /l/); // throws TypeError