String.prototype.replaceAll()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 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"));
// 予想される結果: "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"));
// 予想される結果: "I think Ruth's ferret is cuter than your ferret!"
構文
replaceAll(pattern, replacement)
引数
pattern-
文字列または
Symbol.replaceメソッドを持つオブジェクトを置くことができます。典型的な例は正規表現です。Symbol.replaceメソッドを持たない値は文字列に変換されます。regexpが正規表現である場合、グローバルフラグ (g) が設定されます。そうでなければTypeErrorが発生します。 replacement-
文字列または関数を指定することができます。この置換は
String.prototype.replace()と意味的に同じです。
返値
パターンに一致したすべての文字列を置換文字列で置き換えた新しい文字列です。
例外
解説
このメソッドは呼び出された文字列値を変更しません。新しい文字列を返します。
replace() とは異なり、このメソッドは、最初の文字列だけでなく、出現するすべての文字列を置き換えます。 replace() を RegExp() で動的に構築したグローバル正規表現と共に使用して、文字列のすべての出現を置き換えることも可能ですが、文字列に正規表現で意味を持つ特殊文字が含まれている場合(置換文字列がユーザー入力によるものである場合など)、意図しない結果になる可能性があります。RegExp.escape() を使用して正規表現文字列をリテラルパターンに変換することで、この問題を軽減することは可能ですが、文字列を正規表現に変換せずに replaceAll() に直接渡す方がより簡単です。
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 が空文字列の場合、split() の動作と同様に、UTF-16 のコード単位ごとに置換文字列が挿入されます。
"xxx".replaceAll("", "_"); // "_x_x_x_"
正規表現プロパティ(特に sticky フラグ)と replaceAll() との相互作用については、RegExp.prototype[Symbol.replace]() を参照してください。
例
>replaceAll() の使用
"aabbcc".replaceAll("b", ".");
// 'aa..cc'
グローバルではない正規表現
正規表現フラグを使用する場合は、グローバルである必要があります。これは動作しません。
"aabbcc".replaceAll(/b/, ".");
// TypeError: replaceAll must be called with a global RegExp
これは動作します。
"aabbcc".replaceAll(/b/g, ".");
("aa..cc");
仕様書
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-string.prototype.replaceall> |
ブラウザーの互換性
Loading…