Symbol.replace
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2020å¹´1æ.
Symbol.replace ã¯éçãã¼ã¿ããããã£ã§ãã¦ã§ã«ãã¦ã³ã·ã³ãã«ã® Symbol.replace ã表ãã¾ããString.prototype.replace() ããã³ String.prototype.replaceAll() ã¡ã½ããã¯ç¬¬ 1 弿°ã§ãç¾å¨ã®ãªãã¸ã§ã¯ãã«ä¸è´ããé¨åæååãç½®ãæããã¡ã½ãããããã®ã·ã³ãã«ã§æ¢ãã¾ãã
詳ããã¯ãRegExp.prototype[Symbol.replace]()ãString.prototype.replace()ãString.prototype.replaceAll() ãåç
§ãã¦ãã ããã
試ãã¦ã¿ã¾ããã
class Replace1 {
constructor(value) {
this.value = value;
}
[Symbol.replace](string) {
return `s/${string}/${this.value}/g`;
}
}
console.log("foo".replace(new Replace1("bar")));
// äºæ³ãããçµæ: "s/foo/bar/g"
å¤
ã¦ã§ã«ãã¦ã³ã·ã³ãã« Symbol.replace ã§ãã
Symbol.replace ã®ããããã£å±æ§ | |
|---|---|
| æ¸è¾¼å¯è½ | ä¸å¯ |
| åæå¯è½ | ä¸å¯ |
| è¨å®å¯è½ | ä¸å¯ |
ä¾
>Symbol.replace ã®ä½¿ç¨
js
class CustomReplacer {
constructor(value) {
this.value = value;
}
[Symbol.replace](string) {
return string.replace(this.value, "#!@?");
}
}
console.log("football".replace(new CustomReplacer("foo"))); // "#!@?tball"
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-symbol.replace> |