Baseline Widely available
matchAll()
㯠String
å¤ã®ã¡ã½ããã§ããã®æååã¨æ£è¦è¡¨ç¾ãç
§åãããã¹ã¦ã®çµæãããã£ããã£ã°ã«ã¼ããå«ã¿ãã¤ãã¬ã¼ã¿ã¼ã§è¿ãã¡ã½ããã§ãã
const regexp = /t(e)(st(\d?))/g;
const str = "test1test2";
const array = [...str.matchAll(regexp)];
console.log(array[0]);
// Expected output: Array ["test1", "e", "st1", "1"]
console.log(array[1]);
// Expected output: Array ["test2", "e", "st2", "2"]
æ§æ 弿°
regexp
æ£è¦è¡¨ç¾ãªãã¸ã§ã¯ããã¾ã㯠Symbol.matchAll
ãæã¤ä»»æã®ãªãã¸ã§ã¯ãã§ãã
regexp
ã RegExp
以å¤ã®ãªãã¸ã§ã¯ãã§ãã£ãå ´åãæé»çã« RegExp
ã¸ã®å¤æã new RegExp(regexp, 'g')
ã使ç¨ãã¦è¡ããã¾ãã
regexp
ãæ£è¦è¡¨ç¾ã§ããå ´åãã°ãã¼ãã«ãã©ã° (g
) ãè¨å®ããã¾ããããã§ãªããã° TypeError
ãçºçãã¾ãã
ä¸è´ãããã®ã®å復å¯è½ãªã¤ãã¬ã¼ã¿ã¼ãªãã¸ã§ã¯ãï¼åèµ·åä¸å¯è½ãªãã®ï¼ãã¾ãã¯ä¸è´ãããã®ããªããã°ç©ºã®ã¤ãã¬ã¼ã¿ã¼ã§ããã¤ãã¬ã¼ã¿ã¼ãçæããããããã®å¤ã¯ãRegExp.prototype.exec()
ã®è¿å¤ã¨åãå½¢ã§ãã
TypeError
regexp
ãæ£è¦è¡¨ç¾ã§ããå ´åã§ãã°ãã¼ãã«ãã©ã° (g
) ãè¨å®ããã¦ããªãå ´åï¼flags
ããããã£ã« "g"
ãå«ã¾ãã¦ããªãå ´åï¼ã
String.prototype.matchAll
ã®å®è£
èªä½ã¯ãæ£è¦è¡¨ç¾ãã°ãã¼ãã«ã§ããã¨ããä½åãªå
¥åæ¤è¨¼ãé¤ãã°ï¼é常ã«ã·ã³ãã«ã§ã弿°ã®æååãæåã®å¼æ°ã¨ã㦠Symbol.matchAll
ã¡ã½ãããå¼ã³åºãã ãã§ããå®éã®å®è£
㯠RegExp.prototype[Symbol.matchAll]()
ããæ¥ã¦ãã¾ãã
matchAll()
ã JavaScript ã«è¿½å ãããåã¯ã regexp.execï¼ããã³ /g
ãã©ã°ä»ãã®æ£è¦è¡¨ç¾ï¼ãã«ã¼ãã®ä¸ã§å¼ã³åºããã¨ã§ãã¹ã¦ã®ä¸è´çµæãåå¾ãããã¨ãã§ãã¾ããã
const regexp = /foo[a-z]*/g;
const str = "table football, foosball";
let match;
while ((match = regexp.exec(str)) !== null) {
console.log(
`Found ${match[0]} start=${match.index} end=${regexp.lastIndex}.`,
);
}
// Found football start=6 end=14.
// Found foosball start=16 end=24.
matchAll()
ã使ããããã«ãªã£ããã¨ã§ã while
ã«ããã«ã¼ãã¨ãg
ä»ãã® exec
ãé¿ãããã¨ãã§ãã¾ãã代ããã«ã¤ãã¬ã¼ã¿ã¼ãåå¾ã§ããã®ã§ãfor...of
ãé
åã¹ãã¬ãããArray.from()
æ§é ã¨å¹çããçµã¿åããããã¨ãã§ãã¾ãã
const regexp = /foo[a-z]*/g;
const str = "table football, foosball";
const matches = str.matchAll(regexp);
for (const match of matches) {
console.log(
`Found ${match[0]} start=${match.index} end=${
match.index + match[0].length
}.`,
);
}
// Found football start=6 end=14.
// Found foosball start=16 end=24.
// ä¸è´ããã¤ãã¬ã¼ã¿ã¼ã¯ for...of ã®å復å¦çã®å¾ã§å©ç¨ä¸å¯ã«ãªã
// æ°ããã¤ãã¬ã¼ã¿ã¼ã使ããããã« matchAll ãå度å¼ã³åºã
Array.from(str.matchAll(regexp), (m) => m[0]);
// [ "football", "foosball" ]
matchAll
ã¯ãã°ãã¼ãã« (g
) ãã©ã°ããªãå ´åã¯ä¾å¤ãçºçãã¾ãã
const regexp = /[a-c]/;
const str = "abc";
str.matchAll(regexp);
// TypeError
matchAll
ã§ã¯å
é¨çã« regexp
ã®è¤è£½ã使ãã¾ãããã®ãã regexp.exec()
ã¨ã¯éã£ã¦æååãã¹ãã£ã³ããéã« 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" ]
ãããããã㯠regexp.exec()
ãã«ã¼ãå
ã§ä½¿ãã®ã¨ã¯ç°ãªããæ£è¦è¡¨ç¾ãé²ãããæ»ãããããããã« lastIndex
ã夿´ãããã¨ãã§ããªããã¨ãæå³ãã¾ãã
matchAll
ã¯ãã£ããã£ã°ã«ã¼ãã¸ã®ããããã¢ã¯ã»ã¹ãå®ç¾ãã¾ãã
match()
ã§ã¯ãã°ãã¼ãã« g
ãã©ã°ã使ç¨ããã¨ãã£ããã£ã°ã«ã¼ããç¡è¦ããã¦ãã¾ãã¾ãã
const regexp = /t(e)(st(\d?))/g;
const str = "test1test2";
str.match(regexp); // ['test1', 'test2']
matchAll
ã使ãã°ç°¡åã«ãã£ããã£ã°ã«ã¼ãã«ã¢ã¯ã»ã¹ã§ãã¾ãã
const array = [...str.matchAll(regexp)];
array[0];
// ['test1', 'e', 'st1', '1', index: 0, input: 'test1test2', length: 4]
array[1];
// ['test2', 'e', 'st2', '2', index: 5, input: 'test1test2', length: 4]
matchAll() ã RegExp ã§ã¯ãªã [Symbol.matchAll]()
ãå®è£
ãã¦ãããªãã¸ã§ã¯ãã§ä½¿ç¨
ãªãã¸ã§ã¯ãã« Symbol.matchAll
ã¡ã½ãããããã°ããããã«ã¹ã¿ã ãããã£ã¼ã¨ãã¦ä½¿ããã¨ãã§ãã¾ããSymbol.matchAll
ã®è¿å¤ã¯ matchAll()
ã®è¿å¤ã¨ãªãã
const str = "Hmm, this is interesting.";
str.matchAll({
[Symbol.matchAll](str) {
return [["Yes, it's interesting."]];
},
}); // [["Yes, it's interesting."]] ãè¿ã
仿§æ¸ ãã©ã¦ã¶ã¼ã®äºææ§ é¢é£æ
å ±
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