Baseline Widely available
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]()
를 참조íì¸ì.
"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