Перевод не завершен. Пожалуйста, помогите перевести эту статью с английского.
Метод matchAll() возвращает итератор по всем результатам при сопоставлении строки с регулярным выражением.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
str.matchAll(regexp)
Parameters
regexp- A regular expression object. If a non-RegExp object
objis passed, it is implicitly converted to aRegExpby usingnew RegExp(obj).
Return value
An iterator (which is not a restartable iterable).
Examples
Regexp.exec() and matchAll()
Prior to the addition of matchAll to JavaScript, it was possible to use calls to regexp.exec (and regexes with the /g flag) in a loop to obtain all the matches:
const regexp = RegExp('foo*','g');
const str = 'table football, foosball';
while ((matches = regexp.exec(str)) !== null) {
console.log(`Found ${matches[0]}. Next starts at ${regexp.lastIndex}.`);
// expected output: "Found foo. Next starts at 9."
// expected output: "Found foo. Next starts at 19."
}
With matchAll available, you can avoid the while loop and exec with /g.
Instead, by using matchAll, you get back an iterator which you can use with the more convenient for...of, array spread, or Array.from() constructs:
const regexp = RegExp('foo*','g');
const str = 'table football, foosball';
let matches = str.matchAll(regexp);
for (const match of matches) {
console.log(match);
}
// Array [ "foo" ]
// Array [ "foo" ]
// matches iterator is exhausted after the for..of iteration
// Call matchAll again to create a new iterator
matches = str.matchAll(regexp);
Array.from(matches, m => m[0]);
// Array [ "foo", "foo" ]
Better access to capturing groups
Another compelling reason for matchAll is the improved access to capture groups. Capture groups are ignored when using match() with the global /g flag:
var regexp = /t(e)(st(\d?))/g; var str = 'test1test2'; str.match(regexp); // Array ['test1', 'test2']
With matchAll you can access them:
let 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]
Specifications
| Specification | Status |
|---|---|
| String.prototype.matchAll | Stage 3 |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| Компьютеры | Мобильные | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
matchAll | Chrome Полная поддержка 73 | Edge Нет поддержки Нет | Firefox Полная поддержка 67 | IE Нет поддержки Нет | Opera Полная поддержка 60 | Safari Нет поддержки Нет | WebView Android Полная поддержка 73 | Chrome Android Полная поддержка 73 | Edge Mobile Нет поддержки Нет | Firefox Android Полная поддержка 67 | Opera Android Полная поддержка Да | Safari iOS Нет поддержки Нет | Samsung Internet Android Полная поддержка Да | nodejs Нет поддержки Нет |
Легенда
- Полная поддержка
- Полная поддержка
- Нет поддержки
- Нет поддержки