@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 regular expression searchValue (identified like the native IsRegExp, so a RegExp whose Symbol.match is falsy isn't one and an object whose Symbol.match is truthy is) must have a global (g) flag in its flags; a non-global one throws a TypeError.
      • A string searchValue is treated as a literal pattern (special regex characters are escaped) and all occurrences are replaced. Like the native function the value and the searchValue are converted to strings (a Symbol throws a TypeError).
      • If searchValue has a [Symbol.replace] (read once, a value which isn't null, undefined or a function throws a TypeError), it is called with the (unconverted) value and replaceValue, like the native function. On a runtime without a native Symbol (eg. IE) the method for the polyfill symbol (getKnownSymbol(WellKnownSymbols.replace)) is called, and on a runtime without Symbol.replace a RegExp is used like its native [Symbol.replace].
      • 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 the $&, $1…$n and $' patterns, or $ followed by a backtick for the text before the match) 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)