As of August 2020 the 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)
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.

描述

This method does not change the calling String object. It simply returns a new string.

Specifying a string as a parameter

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.

Specifying a function as a parameter

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.)

范例

Using replaceAll

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

Non-global regex throws

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.

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
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

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. String
  3. 特性
    1. String 长度
  4. 方法
    1. String.fromCharCode()
    2. String.fromCodePoint()
    3. String.prototype.anchor()
    4. String.prototype.big()
    5. String.prototype.blink()
    6. String.prototype.bold()
    7. String.prototype.charAt()
    8. String.prototype.charCodeAt()
    9. String.prototype.codePointAt()
    10. String.prototype.concat()
    11. String.prototype.endsWith()
    12. String.prototype.fixed()
    13. String.prototype.fontcolor()
    14. String.prototype.fontsize()
    15. String.prototype.includes()
    16. String.prototype.indexOf()
    17. String.prototype.italics()
    18. String.prototype.lastIndexOf()
    19. String.prototype.link()
    20. String.prototype.localeCompare()
    21. String.prototype.match()
    22. String.prototype.matchAll()
    23. String.prototype.normalize()
    24. String.prototype.padEnd()
    25. String.prototype.padStart()
    26. String.prototype.repeat()
    27. String.prototype.replace()
    28. String.prototype.replaceAll()
    29. String.prototype.search()
    30. String.prototype.slice()
    31. String.prototype.small()
    32. String.prototype.split()
    33. String.prototype.startsWith()
    34. String.prototype.strike()
    35. String.prototype.sub()
    36. String.prototype.substr()
    37. String.prototype.substring()
    38. String.prototype.sup()
    39. String.prototype.toLocaleLowerCase()
    40. String.prototype.toLocaleUpperCase()
    41. String.prototype.toLowerCase()
    42. String.prototype.toSource()
    43. String.prototype.toString()
    44. String.prototype.toUpperCase()
    45. String.prototype.trim()
    46. String.prototype.trimEnd()
    47. String.prototype.trimStart()
    48. String.prototype.valueOf()
    49. String.prototype[@@iterator]()
    50. String.raw()
  5. 继承:
  6. Function
  7. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  8. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  9. Object
  10. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  11. 方法
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toSource()
    10. Object.prototype.toString()
    11. Object.prototype.valueOf()
    12. Object.setPrototypeOf()