JavaScript String replace() Method

Definition and Usage

replace() The method searches for a value or a regular expression in the string.

replace() The method returns a new string with the replaced value.

replace() The method does not change the original string.

Tip:If you replace the value, it will only replace the first instance. To replace all instances, please use the one with g Regular expressions for modifier sets.

See also:

Regular Expression Tutorial

Regular Expression Reference Manual

Instance

Example 1

Replace Microsoft:

let text = "Visit Microsoft!";
let result = text.replace("Microsoft", "W3School");

try it yourself

Example 2

Global replacement:

let text = "Mr Blue has a blue house and a blue car";
let result = text.replace(/blue/g, "red");

try it yourself

Example 3

Global, case-insensitive replacement:

let text = "Mr Blue has a blue house and a blue car";
let result = text.replace(/blue/gi, "red");

try it yourself

Example 4

Return a function that returns the replacement text:

let text = "Mr Blue has a blue house and a blue car";
let result = text.replace(/blue|house|car/gi, function (x) {
  return x.toUpperCase();
});

try it yourself

syntax

string.replace(regexp, replacement)

parameters

parameters description
regexp

required. The value to be searched or the regular expression.

This parameter specifies the RegExp object of the pattern to be replaced.

If this parameter is a string, it will be used as the direct quantity text pattern to be retrieved.

replacement

required. String.

specifies the replacement text or the function that generates the replacement text.

return value

type description
string the new string with the specified value replaced.

technical details

return value

returns a new string, which is replacement replaced regexp the first match or all matches after

indicates

string string in replace() The method performs a search and replace operation. It will string search for regexp matches the substring, then replacement to replace these substrings. If regexp if the global flag g is present, then replace() the method will replace all the matched substrings. Otherwise, it only replaces the first matched substring.

replacement can be a string or a function. If it is a string, then each match will be replaced by the string. However replacement in $ characters have specific meanings. As shown in the following table, it explains that the string obtained from the pattern matching will be used for replacement.

characters replacement text
$1, $2, ..., $99 matches regexp the text that matches the first to the 99th subexpression in
$& matches regexp the matched substring.
$` text located to the left of the matched substring.
$' text located to the right of the matched substring.
$$ direct quantity symbols.

Note:ECMAScript v3 specifies thatreplace() the method's parameters replacement it can be a function instead of a string. In this case, the function is called for each match, and the string returned by the function is used as the replacement text. The first parameter of the function is the string of the matching pattern. The subsequent parameters are the strings that match the subexpressions in the pattern, and there can be 0 or more such parameters. The next parameter is an integer that declares the position of the match in string at the position in the string. The last parameter is string itself.

Browser support

replace() It is an ECMAScript1 (ES1) feature.

All browsers fully support ES1 (JavaScript 1997):

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Soporte Soporte Soporte Soporte Soporte Soporte

Páginas relacionadas

Cadena de caracteres en JavaScript

Métodos de cadena en JavaScript

Búsqueda de cadenas en JavaScript