Back to Functions

replace

string

Replaces matches for pattern in string with replacement.

Installation

Import
import { replace } from '@tulx/utils';

Source Code

Implementation
/**
 * Replaces matches for pattern in string with replacement.
 *
 * @param string - The string to modify.
 * @param pattern - The pattern to replace.
 * @param replacement - The string replacement.
 * @returns Returns the modified string.
 *
 * @example
 * ```ts
 * replace('Hi Fred', 'Fred', 'Barney'); // 'Hi Barney'
 * ```
 */
export function replace(
  string: string,
  pattern: string | RegExp,
  replacement: string | ((match: string, ...args: unknown[]) => string)
): string {
  return string.replace(pattern, replacement as string);
}

Example

import { replace } from '@tulx/utils';

replace('Hi Fred', 'Fred', 'Barney'); // 'Hi Barney'

Related Functions