Skip to content

replace

The replace() method returns a new string in which part of the original string is replaced by another string.

⚠️ This method replaces only the first occurrence found. To replace all occurrences, use the replaceAll method.

Interactive example

Syntax

Classic signature

typescript
function replace<
	GenericInput extends string
>(
	input: GenericInput, 
	pattern: string | RegExp, 
	replacement: string
): string;

Curried signature

typescript
function replace<
	GenericInput extends string
>(
	pattern: string | RegExp, 
	replacement: string
): (input: GenericInput) => string;

Parameters

  • input: The string in which to perform the replacement.
  • pattern: The string or regular expression to search for.
  • replacement: The replacement string.

Return value

A new string with the replacements applied.

See also

  • repeat: Repeats a string a specified number of times.
  • replaceAll: Replaces all occurrences in a string.
  • trim: Removes whitespace at the start and end of a string.

Sources

Released under the MIT license.