Metodo replace() della stringa JavaScript

Definizione e uso

replace() Il metodo cerca un valore o un'espressione regolare nella stringa.

replace() Il metodo restituisce una nuova stringa con il valore sostituito.

replace() Il metodo non cambia la stringa originale.

Suggerimento:Se sostituisci il valore, verrà sostituita solo la prima istanza. Per sostituire tutte le istanze, utilizzare la versione con g Espressioni regolari dei set di modificatori.

Vedi anche:

Corso di espressioni regolari

Manuale di riferimento delle espressioni regolari

Esempio

Esempio 1

Sostituisci Microsoft:

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

Try it yourself

Esempio 2

Sostituzione globale:

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

Try it yourself

Esempio 3

Globale, senza distinzione tra maiuscole e minuscole, sostituzione:

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 the function to replace the 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.

Specify 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 after the first match or all matches are obtained.

explains

string string in replace() method performs a search and replace operation. It will be string to find the regexp matching substring, then replacement to replace these substrings. If regexp flag g is present, then replace() method will replace all matching substrings. Otherwise, it only replaces the first matching substring. If

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 table below, it explains that the string obtained from the pattern matching will be used for replacement.

character replacement text
$1, $2, ..., $99 with regexp matches the first 99 subexpressions from the 1st to the 99th.
$& with regexp the matched substring.
$` text to the left of the matched substring.
$' text to the right of the matched substring.
$$ Direct quantity symbols.

Note:ECMAScript v3 specifies that,replace() method's parameters replacement 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 following 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
Supporto Supporto Supporto Supporto Supporto Supporto

Pagine correlate

Stringa JavaScript

Metodi della stringa JavaScript

Ricerca della stringa JavaScript