--- title: String.prototype.replaceAll() slug: Web/JavaScript/Reference/Global_Objects/String/replaceAll tags: - global flag / g - replace - replaceAll translation_of: Web/JavaScript/Reference/Global_Objects/String/replaceAll ---
replaceAll()
方法返回一个新字符串,新字符串所有满足 pattern
的部分都已被replacement
替换。pattern
可以是一个字符串或一个 {{jsxref("RegExp")}}, replacement
可以是一个字符串或一个在每次匹配被调用的函数。
原始字符串保持不变。
const newStr = str.replaceAll(regexp|substr, newSubstr|function)
当使用一个 `regex`时,您必须设置全局(“ g”)标志,
否则,它将引发 TypeError
:“必须使用全局 RegExp 调用 replaceAll”。
regexp
(pattern)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
newSubstr
. It is treated as a literal string and is not interpreted as a regular expression.newSubstr
(replacement)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.
此方法不会更改调用 {{jsxref("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 n th parenthesized submatch string, provided the first argument was a {{jsxref("RegExp")}} object. Note that this is 1 -indexed. |
你可以指定一个函数作为第二个参数,在这种情况下,函数只有在匹配发生之后才会被调用。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 {{jsxref("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 {{jsxref("RegExp")}} object—and, if so, how many parenthesized submatches it specifies.)
'aabbcc'.replaceAll('b', '.'); // 'aa..cc'
使用正则表达式搜索值时,它必须是全局的。这将行不通:
'aabbcc'.replaceAll(/b/, '.'); TypeError: replaceAll must be called with a global RegExp
这将可以正常运行:
'aabbcc'.replaceAll(/b/g, '.'); "aa..cc"
Specification |
---|
{{SpecName('ESDraft', '#sec-string.prototype.replaceall', 'String.prototype.replaceAll')}} |
{{Compat("javascript.builtins.String.replaceAll")}}