[@@replace]()
method replaces some or all matches of a
this
pattern in a string by a
replacement
, and returns the result of the replacement as a new string. The
replacement
can be a string or a function to be called for each match.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
regexp[Symbol.replace](str, newSubStr|function)
str
String
that is a target of the replacement.
newSubStr (replacement)
String
that replaces the substring. A number of special replacement patterns are supported; see the
Specifying a string as a parameter
section in
String.prototype.replace()
页面。
function (replacement)
String.prototype.replace()
页面。
A new string with some or all matches of a pattern replaced by a replacement.
This method is called internally in
String.prototype.replace()
若
pattern
argument is a
RegExp
object. For example, following two examples return same result.
'abc'.replace(/a/, 'A');
/a/[Symbol.replace]('abc', 'A');
This method exists for customizing replace behavior in
RegExp
subclass.
If pattern argument is
not
a
RegExp
对象,
String.prototype.replace()
doesn't call this method, nor creates a
RegExp
对象。
This method can be used in almost the same way as
String.prototype.replace()
, except the different
this
and the different arguments order.
var re = /-/g; var str = '2016-01-01'; var newstr = re[Symbol.replace](str, '.'); console.log(newstr); // 2016.01.01
Subclasses of
RegExp
can override the
[@@replace]()
method to modify the default behavior.
class MyRegExp extends RegExp {
constructor(pattern, flags, count) {
super(pattern, flags);
this.count = count;
}
[Symbol.replace](str, replacement) {
// Perform @@replace |count| times.
var result = str;
for (var i = 0; i < this.count; i++) {
result = RegExp.prototype[Symbol.replace].call(this, result, replacement);
}
return result;
}
}
var re = new MyRegExp('\\d', '', 3);
var str = '01234567';
var newstr = str.replace(re, '#'); // String.prototype.replace calls re[@@replace].
console.log(newstr); // ###34567
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'RegExp.prototype[@@replace]' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@replace
|
Chrome 50 | Edge 79 | Firefox 49 | IE No | Opera 37 | Safari 10 | WebView Android 50 | Chrome Android 50 | Firefox Android 49 | Opera Android 37 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 6.0.0 |
完整支持
不支持
String.prototype.replace()
RegExp.prototype[@@match]()
RegExp.prototype[@@search]()
RegExp.prototype[@@split]()
RegExp.prototype.exec()
RegExp.prototype.test()
RegExp
RegExp.$1-$9
RegExp.input ($_)
RegExp.lastMatch ($&)
RegExp.lastParen ($+)
RegExp.leftContext ($`)
RegExp.prototype.dotAll
RegExp.prototype.flags
RegExp.prototype.global
RegExp.prototype.ignoreCase
RegExp.prototype.multiline
RegExp.prototype.source
RegExp.prototype.sticky
RegExp.prototype.unicode
RegExp.rightContext ($')
RegExpInstance.lastIndex
get RegExp[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()