String.prototype.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æâ©.
matchAll() æ¹æ³è¿åä¸ä¸ªè¿ä»£å¨ï¼è¯¥è¿ä»£å¨å
å«äºæ£ç´¢åç¬¦ä¸²ä¸æ£å表达å¼è¿è¡å¹é
çææç»æï¼å
æ¬æè·ç»ï¼ã
å°è¯ä¸ä¸
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"]
è¯æ³
matchAll(regexp)
åæ°
regexp-
ä¸ä¸ªæ£å表达å¼å¯¹è±¡ï¼æè æ¯ä»»ä½å ·æ
Symbol.matchAllæ¹æ³ç对象ã妿
regexp䏿¯ä¸ä¸ªRegExp对象ï¼å¹¶ä¸æ²¡æSymbol.matchAllæ¹æ³ï¼å®å°éè¿new RegExp(regexp, 'g')被éå¼è½¬æ¢ä¸ºä¸ä¸ªRegExp对象ã妿
regexpæ¯ä¸ä¸ªæ£å表达å¼ï¼é£ä¹å®å¿ 须设置äºå ¨å±ï¼gï¼æ å¿ï¼å¦å伿åºTypeErrorå¼å¸¸ã
è¿åå¼
ä¸ä¸ªå¹é
ç»æçå¯è¿ä»£è¿ä»£å¨å¯¹è±¡ï¼å®ä¸å¯éæ°å¼å§ï¼ãæ¯ä¸ªå¹é
ç»æé½æ¯ä¸ä¸ªæ°ç»ï¼å
¶å½¢ç¶ä¸ RegExp.prototype.exec() çè¿åå¼ç¸åã
å¼å¸¸
TypeError-
妿
regexpæ¯ä¸ä¸ªæ£å表达å¼ï¼ä¸æ²¡æè®¾ç½®å ¨å±ï¼gï¼æ å¿ï¼å ¶flags屿§ä¸å å«"g"ï¼ï¼å伿åºè¯¥å¼å¸¸ã
æè¿°
String.prototype.matchAll æ¹æ³æ¬èº«çå®ç°é常ç®åï¼å®åªæ¯è°ç¨äºåæ°ç Symbol.matchAll æ¹æ³ï¼å¹¶å°å符串ä½ä¸ºç¬¬ä¸ä¸ªåæ°ä¼ éäºè¿å»ï¼é¤äºé¢å¤çè¾å
¥éªè¯ï¼å³æ£å表达å¼å¿
é¡»æ¯å
¨å±çï¼ãå®é
çå®ç°æ¥èª RegExp.prototypeSymbol.matchAll]()ã
示ä¾
>Regexp.prototype.exec() å matchAll()
å¦ææ²¡æ matchAll() æ¹æ³ï¼ä»ç¶å¯ä»¥ä½¿ç¨å¸¦æ g æ å¿çæ£å表达å¼è°ç¨ regexp.exec() æ¥å¨å¾ªç¯ä¸è·åææå¹é
ç»æï¼
const regexp = /foo[a-z]*/g;
const str = "table football, foosball";
let match;
while ((match = regexp.exec(str)) !== null) {
console.log(
`æ¾å° ${match[0]} èµ·å§ä½ç½®=${match.index} ç»æä½ç½®=${regexp.lastIndex}ã`,
);
}
// æ¾å° football èµ·å§ä½ç½®=6 ç»æä½ç½®=14ã
// æ¾å° foosball èµ·å§ä½ç½®=16 ç»æä½ç½®=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(
`æ¾å° ${match[0]} èµ·å§ä½ç½®=${match.index} ç»æä½ç½®=${
match.index + match[0].length
}.`,
);
}
// æ¾å° football èµ·å§ä½ç½®=6 ç»æä½ç½®=14.
// æ¾å° foosball èµ·å§ä½ç½®=16 ç»æä½ç½®=24.
// å¹é
è¿ä»£å¨å¨ for...of è¿ä»£åç¨å°½
// 忬¡è°ç¨ matchAll 以å建æ°çè¿ä»£å¨
Array.from(str.matchAll(regexp), (m) => m[0]);
// [ "football", "foosball" ]
å¦ææ²¡æ g æ å¿ï¼matchAll 伿åºå¼å¸¸ã
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 æ¥ä½¿æ£å表达å¼åè¿æåéã
æ¯ String.prototype.match() æ´å¥½çæè·ç»è·åæ¹å¼
matchAll() æ¹æ³çå¦ä¸ä¸ªéè¦ä¼ç¹æ¯æ¹è¿äºå¯¹äºæè·ç»çè·åæ¹å¼ã
å½ä½¿ç¨å
¨å± g æ å¿è°ç¨ match() æ¹æ³æ¶ï¼æè·ç»ä¼è¢«å¿½ç¥ï¼
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]
使ç¨å®ç°äº [Symbol.matchAll]() ç鿣å对象è°ç¨ matchAll()
妿ä¸ä¸ªå¯¹è±¡æä¸ä¸ª Symbol.matchAll æ¹æ³ï¼å®å¯ä»¥è¢«ç¨ä½èªå®ä¹å¹é
å¨ãSymbol.matchAll çè¿åå¼å°æä¸º matchAll() çè¿åå¼ã
const str = "Hmm, this is interesting.";
str.matchAll({
[Symbol.matchAll](str) {
return [["Yes, it's interesting."]];
},
}); // returns [["Yes, it's interesting."]]
è§è
| Specification |
|---|
| ECMAScript® 2026 Language Specification > # sec-string.prototype.matchall > |