Function multiReplace

  • A string replacement function, but specialized for doing multiple replacements in a single pass through the input string.

    Based off Nim's strutils.multiReplace.

    Time complexity: O(n)

    Space complexity: O(n)

    Parameters

    • string: string

      The string to replace values in

    • replacements: Record<string, string>

      An object of replacements where keys are the search values and values are the replacement values

    Returns string

    A new string with the replacements applied

    String

    multiReplace('a b c', {a: 'c', c: 'a'}); // 'c b a'