replaceAll()
method is supported by Firefox but not by Chrome. It will become available in Chrome 85.
replaceAll()
method returns a new string with all matches of a
pattern
replaced by a
replacement
.
pattern
can be a string or a
RegExp
,和
replacement
can be a string or a function to be called for each match.
The original string is left unchanged.
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.
const newStr = str.replaceAll(regexp|substr, newSubstr|function)
when using a `
regexp
` you must have to set the global ("g") flag; otherwise, it will throw a
TypeError
: "replaceAll must be called with a global RegExp".
regexp
(pattern)
RegExp
object or literal with the global flag. The matches are replaced with
newSubstr
or the value returned by the specified
function
. A RegExp without the global ("g") flag will throw a
TypeError
: "replaceAll must be called with a global RegExp".
substr
String
that is to be replaced by
newSubstr
. It is treated as a literal string and is
not
interpreted as a regular expression.
newSubstr
(replacement)
String
that replaces the substring specified by the specified
regexp
or
substr
parameter. A number of special replacement patterns are supported; see the "
Specifying a string as a parameter
" section below.
function
(replacement)
regexp
or
substr
. The arguments supplied to this function are described in the "
Specifying a function as a parameter
" section below.
A new string, with all matches of a pattern replaced by a replacement.
This method does not change the calling
String
object. It simply returns a new string.
The replacement string can include the following special replacement patterns:
| Pattern | 插入 |
|---|---|
$$
|
Inserts a
"$"
.
|
$&
|
Inserts the matched substring. |
$`
|
Inserts the portion of the string that precedes the matched substring. |
$'
|
Inserts the portion of the string that follows the matched substring. |
$
n
|
Where
n
is a positive integer less than 100, inserts the
n
th parenthesized submatch string, provided the first argument was a
RegExp
object. Note that this is
1
-indexed.
|
You can specify a function as the second parameter. In this case, the function will be invoked after the match has been performed. The function's result (return value) will be used as the replacement string. ( 注意: The above-mentioned special replacement patterns do not apply in this case.)
Note that the function will be invoked multiple times for each full match to be replaced if the regular expression in the first parameter is global.
The arguments to the function are as follows:
| Possible name | Supplied value |
|---|---|
match
|
The matched substring. (Corresponds to
$&
above.)
|
p1, p2, ...
|
n
th string found by a parenthesized capture group, provided the first argument to
replace()
was a
RegExp
object. (Corresponds to
$1
,
$2
, etc. above.) For example, if
/(\a+)(\b+)/
, was given,
p1
is the match for
\a+
,和
p2
for
\b+
.
|
offset
|
The offset of the matched substring within the whole string being examined. (For example, if the whole string was
'abcd'
, and the matched substring was
'bc'
, then this argument will be
1
.)
|
string
|
The whole string being examined. |
(The exact number of arguments depends on whether the first argument is a
RegExp
object—and, if so, how many parenthesized submatches it specifies.)
'aabbcc'.replaceAll('b', '.');
// 'aa..cc'
When using a regular expression search value, it must be global. This won't work:
'aabbcc'.replaceAll(/b/, '.'); TypeError: replaceAll must be called with a global RegExp
This will work:
'aabbcc'.replaceAll(/b/g, '.'); "aa..cc"
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'String.prototype.replaceAll' in that specification. |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
replaceAll
|
Chrome 85 | Edge No | Firefox 77 | IE No | Opera No | Safari 13.1 | WebView Android 85 | Chrome Android 85 | Firefox Android No | Opera Android No | Safari iOS 13.4 | Samsung Internet Android No | nodejs No |
完整支持
不支持
String
String.fromCharCode()
String.fromCodePoint()
String.prototype.anchor()
String.prototype.big()
String.prototype.blink()
String.prototype.bold()
String.prototype.charAt()
String.prototype.charCodeAt()
String.prototype.codePointAt()
String.prototype.concat()
String.prototype.endsWith()
String.prototype.fixed()
String.prototype.fontcolor()
String.prototype.fontsize()
String.prototype.includes()
String.prototype.indexOf()
String.prototype.italics()
String.prototype.lastIndexOf()
String.prototype.link()
String.prototype.localeCompare()
String.prototype.match()
String.prototype.matchAll()
String.prototype.normalize()
String.prototype.padEnd()
String.prototype.padStart()
String.prototype.repeat()
String.prototype.replace()
String.prototype.replaceAll()
String.prototype.search()
String.prototype.slice()
String.prototype.small()
String.prototype.split()
String.prototype.startsWith()
String.prototype.strike()
String.prototype.sub()
String.prototype.substr()
String.prototype.substring()
String.prototype.sup()
String.prototype.toLocaleLowerCase()
String.prototype.toLocaleUpperCase()
String.prototype.toLowerCase()
String.prototype.toSource()
String.prototype.toString()
String.prototype.toUpperCase()
String.prototype.trim()
String.prototype.trimEnd()
String.prototype.trimStart()
String.prototype.valueOf()
String.prototype[@@iterator]()
String.raw()
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()