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]()
ãåç
§ãã¦ãã ããã
"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