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

    Function polyStrReplaceAll

    • Polyfill implementation of String.prototype.replaceAll() that returns a new string with all matches of a pattern replaced by a replacement.

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

      • A RegExp searchValue must carry the global (g) flag; a non-global RegExp throws a TypeError.
      • A string searchValue is treated as a literal pattern (special regex characters are escaped) and all occurrences are replaced.
      • If searchValue exposes a [Symbol.replace] method, that method is called with the coerced string value and replaceValue, mirroring the native delegation behaviour.
      • If replaceValue is a function it is invoked for each match with the matched substring, captured groups, the match index, and the original string.

      Parameters

      • value: string

        The string value to search and replace within.

      • searchValue: string | RegExp

        The value to search for. Can be a string, a global RegExp, or any object with a [Symbol.replace] method.

      • replaceValue: string | ((substring: string, ...args: any[]) => string)

        The replacement string (may use $&, $1$n, $\`` and $'` patterns) or a replacer function called once per match.

      Returns string

      A new string with every occurrence of searchValue replaced by replaceValue.

      0.14.0

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

      TypeError if value is null or undefined.

      // String literal replacement — replaces every occurrence
      polyStrReplaceAll("aabbcc", "b", "x"); // "aaxxcc"
      polyStrReplaceAll("a-b-a", "a", "x"); // "x-b-x"
      // Global RegExp replacement
      polyStrReplaceAll("abc123abc", /abc/g, "X"); // "X123X"
      // Replacer function
      polyStrReplaceAll("abca", "a", (match, offset) => match.toUpperCase() + offset);
      // "A0bcA3"
      // Special regex characters in search string are escaped
      polyStrReplaceAll("a.b.c", ".", "-"); // "a-b-c" (dot is literal)