翻译不完整。 请帮助我们翻译这篇文章!

replaceAll() 方法返回一个新字符串,该字符串的所有满足;模式的匹配,都被用替换者;替换了。 模式 可以是一个字符串或者一个RegExp, 并且替代者可以是一个字符串,也可以是每次匹配都要调用的函数。



const newStr = str.replaceAll(regexp|substr, newSubstr|function)

当使用一个 `regex`时,您必须设置全局(“ g”)标志,


regexp (pattern)
A 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".
A 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)
The 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)
A function to be invoked to create the new substring to be used to replace the matches to the given 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.


此方法不会更改调用 String 对象。它只是返回一个新字符串。


The replacement string can include the following special replacement patterns:

Pattern Inserts
$$ 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 nth 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. (Note: 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, ... The nth 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+, and 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.)


使用 replaceAll

'aabbcc'.replaceAll('b', '.'); 
// ''

非全局 regex 抛出


'aabbcc'.replaceAll(/b/, '.');
TypeError: replaceAll must be called with a global RegExp


'aabbcc'.replaceAll(/b/g, '.');


ECMAScript (ECMA-262)


Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
replaceAllChrome Full support 85Edge No support NoFirefox Full support 77IE No support NoOpera No support NoSafari Full support 13.1WebView Android Full support 85Chrome Android Full support 85Firefox Android No support NoOpera Android No support NoSafari iOS Full support 13.4Samsung Internet Android No support Nonodejs No support No


Full support  
Full support
No support  
No support