String.prototype.replaceAll()

Baseline 已广泛支持

此功能已成熟,可在许多设备和浏览器版本上使用。它自 ⁨2020 年 8 月⁩ 起在各浏览器中均可使用。

replaceAll() 方法用于 String 值,它会返回一个新的字符串,其中所有匹配 pattern 的部分都被 replacement 替换。pattern 可以是字符串或 RegExp,而 replacement 可以是字符串或一个为每个匹配项调用的函数。原始字符串将保持不变。

试一试

const paragraph = "I think Ruth's dog is cuter than your dog!";

console.log(paragraph.replaceAll("dog", "monkey"));
// Expected output: "I think Ruth's monkey is cuter than your monkey!"

// Global flag required when calling replaceAll with regex
const regex = /dog/gi;
console.log(paragraph.replaceAll(regex, "ferret"));
// Expected output: "I think Ruth's ferret is cuter than your ferret!"

语法

js
replaceAll(pattern, replacement)

参数

pattern

可以是字符串或具有 Symbol.replace 方法的对象 — 最常见的例子是 正则表达式。任何不具有 Symbol.replace 方法的值都将被强制转换为字符串。

如果 pattern 是正则表达式,则必须设置全局(g)标志,否则将抛出 TypeError

replacement

可以是字符串或函数。替换的语义与 String.prototype.replace() 的语义相同。

返回值

一个新的字符串,其中所有匹配模式的部分都已替换为指定的替换项。

异常

TypeError

如果 pattern 是正则表达式但未设置全局(g)标志(其 flags 属性不包含 "g")时抛出。

描述

此方法不会修改其调用的字符串值。它会返回一个新字符串。

replace() 不同,此方法会替换字符串中的所有出现项,而不仅仅是第一个。虽然也可以使用动态构建的全局正则表达式与 RegExp() 来结合使用 replace() 以替换字符串中的所有实例,但如果字符串包含在正则表达式中有特殊含义的字符(这可能会在替换字符串来自用户输入时发生),这可能会导致意外的后果。虽然可以使用 RegExp.escape() 来缓解这种情况,将正则表达式字符串转换为字面量模式,但更简单的方法是直接将字符串传递给 replaceAll(),而无需将其转换为正则表达式。

js
function unsafeRedactName(text, name) {
  return text.replace(new RegExp(name, "g"), "[REDACTED]");
}
function semiSafeRedactName(text, name) {
  return text.replaceAll(name, "[REDACTED]");
}
function superSafeRedactName(text, name) {
  // only match at word boundaries
  return text.replaceAll(
    new RegExp(`\\b${RegExp.escape(name)}\\b`, "g"),
    "[REDACTED]",
  );
}

let report =
  "A hacker called ha.*er used special characters in their name to breach the system.";

console.log(unsafeRedactName(report, "ha.*er")); // "A [REDACTED]s in their name to breach the system."
console.log(semiSafeRedactName(report, "ha.*er")); // "A hacker called [REDACTED] used special characters in their name to breach the system."

report = "A hacker called acke breached the system.";

console.log(semiSafeRedactName(report, "acke")); // "A h[REDACTED]r called [REDACTED] breached the system."
console.log(superSafeRedactName(report, "acke")); // "A hacker called [REDACTED] breached the system."

如果 pattern 是一个具有 Symbol.replace 方法的对象(包括 RegExp 对象),则该方法将使用目标字符串和 replacement 作为参数进行调用。其返回值将成为 replaceAll() 的返回值。在这种情况下,replaceAll() 的行为完全由 [Symbol.replace]() 方法编码,因此将与 replace() 产生相同的结果(除了对正则表达式是否全局的额外输入验证)。

如果 pattern 是一个空字符串,则替换项将插入到每个 UTF-16 码单元之间,这类似于 split() 的行为。

js
"xxx".replaceAll("", "_"); // "_x_x_x_"

有关正则表达式属性(特别是 粘性标志)如何与 replaceAll() 交互的更多信息,请参阅 RegExp.prototype[Symbol.replace]()

示例

使用 replaceAll()

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

非全局正则表达式抛出错误

使用正则表达式作为搜索值时,它必须是全局的。这行不通

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

这将起作用

js
"aabbcc".replaceAll(/b/g, ".");
("aa..cc");

规范

规范
ECMAScript® 2026 语言规范
# sec-string.prototype.replaceall

浏览器兼容性

另见