A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll below:

String.prototype.replaceAll() - JavaScript | MDN

String.prototype.replaceAll()

Baseline Widely available

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!"
構文
replaceAll(pattern, replacement)
引数
pattern

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

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

replacement

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

返値

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

例外
TypeError

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

解説

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

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

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

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");
仕様書 ブラウザーの互換性 関連情報

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4