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() ë©ìëë 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ë©ìëê° ìë 모ë ê°ì 문ìì´ë¡ ê°ì ë³íë©ëë¤.patternì´ ì ê·ìì´ë©´ ì ì(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()ì ëìì @@replace ë©ìëì ìí´ ìì í ì¸ì½ë©ëë¯ë¡ replace()ì ëì¼í 결과를 ê°ê² ë©ëë¤(ì ê·ìì´ ì ìì´ë¼ë ì¶ê° ì
ë ¥ ì í¨ì± ê²ì¬ë ì ì¸).
patternì´ ë¹ ë¬¸ìì´ì¸ ê²½ì°ì split() ëìê³¼ ì ì¬íê² ëª¨ë UTF-16 ì½ë ë¨ì ì¬ì´ì êµì²´ìê° ì½ì
ë©ëë¤.
"xxx".replaceAll("", "_"); // "_x_x_x_"
regex ìì±(í¹í sticky íëê·¸)ì´ replaceAll()ê³¼ ìí¸ ìì©íë ë°©ìì ëí ìì¸í ë´ì©ì RegExp.prototype[@@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® 2027 Language Specification> # sec-string.prototype.replaceall> |