RegExp.prototype[Symbol.matchAll]()
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.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("-\\d+", "g");
console.log("2016-01-02|2019-03-07".matchAll(re));
// äºæ³ãããçµæ: Array [Array ["-01"], Array ["-02"], Array ["-03"], Array ["-07"]]
æ§æ
regexp[Symbol.matchAll](str)
弿°
è¿å¤
ä¸è´ãããã®ã表ãå復å¯è½ãªã¤ãã¬ã¼ã¿ã¼ãªãã¸ã§ã¯ãï¼åèµ·åä¸å¯ï¼ã§ããããããã®ä¸è´é¨å㯠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 = /\d+/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("(\\d+)-(\\d+)-(\\d+)", "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" ]
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-regexp-prototype-%symbol.matchall%> |
ãã©ã¦ã¶ã¼ã®äºææ§
é¢é£æ å ±
RegExp.prototype[Symbol.matchAll]ã®ããªãã£ã« (core-js)- es-shims ã«ãã
RegExp.prototype[Symbol.matchAll]ã®ããªãã£ã« String.prototype.matchAll()RegExp.prototype[Symbol.match]()RegExp.prototype[Symbol.replace]()RegExp.prototype[Symbol.search]()RegExp.prototype[Symbol.split]()Symbol.matchAll