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
camelCase
Converts string to camel case.
capitalize
Converts the first character of string to upper case and the remaining to lower case.
deburr
Deburrs string by converting Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and removing combining diacritical marks.
endsWith
Checks if string ends with the given target string.
escape
Converts the characters "&", "<", ">", '"', and "'" in string to their corresponding HTML entities.
escapeRegExp
Escapes the RegExp special characters "^", "$", "\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in string.