Baseline Widely available
[Symbol.matchAll]()
㯠RegExp
ã¤ã³ã¹ã¿ã³ã¹ã®ã¡ã½ããã§ã String.prototype.matchAll
ãã©ã®ããã«åä½ããã®ããæå®ãã¾ãã
class MyRegExp extends RegExp {
[Symbol.matchAll](str) {
const result = RegExp.prototype[Symbol.matchAll].call(this, str);
if (!result) {
return null;
}
return Array.from(result);
}
}
const re = new MyRegExp("-[0-9]+", "g");
console.log("2016-01-02|2019-03-07".matchAll(re));
// Expected output: Array [Array ["-01"], Array ["-02"], Array ["-03"], Array ["-07"]]
æ§æ
regexp[Symbol.matchAll](str)
弿°
str
æåå (String
) ã§ãç
§åã®å¯¾è±¡ã¨ãªããã®ã§ãã
ä¸è´ãããã®ã表ãå復å¯è½ãªã¤ãã¬ã¼ã¿ã¼ãªãã¸ã§ã¯ãï¼åèµ·åä¸å¯ï¼ã§ããããããã®ä¸è´é¨å㯠RegExp.prototype.exec()
ã®è¿å¤ã¨åãå½¢ã®é
åã§ãã
ãã®ã¡ã½ããã¯å
é¨çã« String.prototype.matchAll()
ãå¼ã³åºãã¾ããä¾ãã°ã以ä¸ã® 2 ã¤ã®ä¾ã¯åãçµæãè¿ãã¾ãã
"abc".matchAll(/a/g);
/a/g[Symbol.matchAll]("abc");
Symbol.split
ã¨åæ§ã [Symbol.matchAll]()
㯠Symbol.species
ã使ç¨ãã¦æ°ããæ£è¦è¡¨ç¾ã使ããã¨ããããå§ããä½ããã£ã¦ãå
ã®æ£è¦è¡¨ç¾ã夿´ãããã¨ãé¿ãã¾ãã lastIndex
ã¯å
ã®æ£è¦è¡¨ç¾ã®å¤ããå§ã¾ãã¾ãã
const regexp = /[a-c]/g;
regexp.lastIndex = 1;
const str = "abc";
Array.from(str.matchAll(regexp), (m) => `${regexp.lastIndex} ${m[0]}`);
// [ "1 b", "1 c" ]
å
¥åãã°ãã¼ãã«æ£è¦è¡¨ç¾ã§ãããã©ããã®æ¤è¨¼ã¯ String.prototype.matchAll()
ã§è¡ããã¾ãã[Symbol.matchAll]()
ã¯å
¥åãæ¤è¨¼ãã¾ãããæ£è¦è¡¨ç¾ãã°ãã¼ãã«ã§ãªãå ´åãè¿ãããã¤ãã¬ã¼ã¿ã¼ã¯ exec()
ã®çµæãä¸åº¦è¿ãããã®å¾ undefined
ãè¿ãã¾ããæ£è¦è¡¨ç¾ãã°ãã¼ãã«ã§ããå ´åãè¿ãããã¤ãã¬ã¼ã¿ã¼ã® next()
ã¡ã½ãããå¼ã³åºããããã³ã«ãæ£è¦è¡¨ç¾ã® exec()
ãå¼ã³åºããçµæãè¿ãã¾ãã
æ£è¦è¡¨ç¾ãç²ççã§ã°ãã¼ãã«ãªå ´åãç²ççãªç
§åãè¡ãã¾ããã¤ã¾ã lastIndex
以éã¯ç
§åãã¾ããã
console.log(Array.from("ab-c".matchAll(/[abc]/gy)));
// [ [ "a" ], [ "b" ] ]
ç¾å¨ã®ç
§åã空æååã®å ´åãlastIndex
ãé²ã¿ã¾ããæ£è¦è¡¨ç¾ã« u
ãã©ã°ãããå ´åãUnicode ã³ã¼ããã¤ã³ã 1 ã¤åé²ã¿ã¾ãã
console.log(Array.from("ð".matchAll(/(?:)/g)));
// [ [ "" ], [ "" ], [ "" ] ]
console.log(Array.from("ð".matchAll(/(?:)/gu)));
// [ [ "" ], [ "" ] ]
ãã®ã¡ã½ãã㯠RegExp
ãµãã¯ã©ã¹ã§ matchAll()
ã®åä½ãã«ã¹ã¿ãã¤ãºããããã«åå¨ãã¾ãã
ãã®ã¡ã½ãã㯠String.prototype.matchAll()
, ã¨ã»ã¼åæ§ã«ä½¿ç¨ãããã¨ãã§ãã¾ããã this
ã®å¤ã¨å¼æ°ã®é åºãéãç¹ãç°ãªãã¾ãã
const re = /[0-9]+/g;
const str = "2016-01-02";
const result = re[Symbol.matchAll](str);
console.log(Array.from(result, (x) => x[0]));
// [ "2016", "01", "02" ]
ãµãã¯ã©ã¹ã§ã® [Symbol.matchAll]()
ã®ä½¿ç¨
RegExp
ã®ãµãã¯ã©ã¹ã¯ [Symbol.matchAll]()
ã¡ã½ããã䏿¸ããã¦æ¢å®ã®åä½ã夿´ãããã¨ãã§ãã¾ãã
ä¾ãã°ã Array
ãã¤ãã¬ã¼ã¿ã¼ã®ä»£ããã«è¿ããã¨ãã§ãã¾ãã
class MyRegExp extends RegExp {
[Symbol.matchAll](str) {
const result = RegExp.prototype[Symbol.matchAll].call(this, str);
return result ? Array.from(result) : null;
}
}
const re = new MyRegExp("([0-9]+)-([0-9]+)-([0-9]+)", "g");
const str = "2016-01-02|2019-03-07";
const result = str.matchAll(re);
console.log(result[0]);
// [ "2016-01-02", "2016", "01", "02" ]
console.log(result[1]);
// [ "2019-03-07", "2019", "03", "07" ]
仿§æ¸ ãã©ã¦ã¶ã¼ã®äºææ§ é¢é£æ
å ±
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