Function replaceString

  • Replaces the matched pattern with the replacement string.

    Parameters

    • target: undefined | string

      The target string.

    • pattern: string | RegExp

      The pattern to match.

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

      The replacement string or a function that returns the replacement string.

    Returns string

    The new string with the matched pattern replaced.

    replace('abcde', 'de', '123'); // 'abc123'
    replace('abcde', /[bd]/g, '-'); // 'a-c-e'
    replace('abcde', 'de', substring => substring.toUpperCase()); // 'abcDE'
    replace('abcde', /[bd]/g, substring => substring.toUpperCase()); // 'aBcDe'