Μέθοδος replace() του JavaScript String

Ορισμός και χρήση

replace() Η μέθοδος αναζητά τιμή ή ακολουθία χαρακτήρων στο字符串.

replace() Η μέθοδος επιστρέφει ένα νέο字符串 με την αντικαταστάθηκε τιμή.

replace() Η μέθοδος δεν αλλάζει το αρχικό字符串.

Σημείωση:Αν αντικαταστήσετε την τιμή, θα αντικατασταθεί μόνο η πρώτη εμφάνιση. Για να αντικαταστήσετε όλες τις εμφανίσεις, χρησιμοποιήστε την g Ακολουθίες χαρακτήρων με διακοσμητικά

Δείτε επίσης:

Εκμάθηση των παραμέτρων της ακολουθίας των χαρακτήρων

Εγχειρίδιο αναφορών για τις παραμέτρους της ακολουθίας των χαρακτήρων

Παράδειγμα

Παράδειγμα 1

Αντικατάσταση Microsoft:

let text = "Επισκεφθείτε το Microsoft!";
let result = text.replace("Microsoft", "W3School");

Try it yourself

Παράδειγμα 2

Παντοδικη αντικατάσταση:

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

Try it yourself

Παράδειγμα 3

Παντοδικη, αδιαφορούσα για τα γράμματα, αντικατάσταση:

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

indicates

string string in replace() method performs a search and replace operation. It will string to find the regexp matches the substring, then replacement to replace these substrings. If regexp has the global flag g, then replace() the 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, the strings obtained from the pattern matching are 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.
$` The text to the left of the matched substring.
$' The text to the right of the matched substring.
$$ Direct quantity symbols.

Note:ECMAScript v3 specifies thatreplace() method 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 match in string at the position found 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
Υποστήριξη Υποστήριξη Υποστήριξη Υποστήριξη Υποστήριξη Υποστήριξη

Σελίδες Σχετικές

String JavaScript

Μέθοδοι String JavaScript

Αναζήτηση String JavaScript