String.prototype.replaceAll()

replaceAll() メソッドは、pattern に一致したすべての文字列を replacement で置き換えた新しい文字列を返します。pattern は文字列または RegExp を指定することができ、replacement は文字列または各一致に対して呼び出される関数を指定することができます。元の文字列は変更されません。

試してみましょう

構文

js
replaceAll(pattern, replacement)

引数

pattern

文字列または Symbol.replace メソッドを持つオブジェクトを置くことができます。典型的な例は正規表現です。Symbol.replace メソッドを持たない値は文字列に変換されます。

regexp正規表現である場合、グローバルフラグ (g) が設定されます。そうでなければ TypeError が発生します。

replacement

文字列または関数を指定することができます。この置換は String.prototype.replace() と意味的に同じです。

返値

パターンに一致したすべての文字列を置換文字列で置き換えた新しい文字列です。

例外

TypeError

pattern正規表現である場合で、グローバルフラグ (g) が設定されていない場合(flags プロパティに "g" が含まれていない場合)。

解説

このメソッドは呼び出された文字列値を変更しません。新しい文字列を返します。

replace() とは異なり、このメソッドは最初に一致した文字列だけでなく、出現した文字列を置き換えます。これは文字列が静的に既知でない場合に特に有用です。特殊文字をエスケープせずに RegExp() コンストラクターを呼び出すと、意図せずに意味づけが変わってしまう可能性があるからです。

js
function unsafeRedactName(text, name) {
  return text.replace(new RegExp(name, "g"), "[REDACTED]");
}
function safeRedactName(text, name) {
  return text.replaceAll(name, "[REDACTED]");
}

const 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(safeRedactName(report, "ha.*er")); // "A hacker called [REDACTED] used special characters in their name to breach the system."

patternSymbol.replace メソッドを持つオブジェクト(RegExp オブジェクトを含む)である場合、そのメソッドは対象の文字列と replacement を引数として呼び出されます。その返値は replaceAll() の返値となります。この場合、replaceAll() の動作は完全に @@replace メソッドによってエンコードされるので、 replace() と同じ結果になります(正規表現がグローバルであるかどうかの余分な入力検証を除けば)。 pattern が空文字列の場合、split() の動作と同様に、UTF-16 のコード単位ごとに置換文字列が挿入されます。

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

正規表現プロパティ(特に sticky フラグ)と replaceAll() との相互作用については、RegExp.prototype[@@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");

仕様書

Specification
ECMAScript Language Specification
# sec-string.prototype.replaceall

ブラウザーの互換性

BCD tables only load in the browser

関連情報