[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll [Back]  [Original]

String.prototype.matchAll() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

String.prototype.matchAll()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2020 ..

matchAll() .

In this article

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"]

str.matchAll(regexp)

regexp

. , , RegExp new RegExp(obj).

iterator ( ).

Regexp.exec() matchAll()

matchAll JavaScript, regexp.exec ( /g ) :

js
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."
}

matchAll, while exec /g. matchAll, , for...of, array spread, Array.from() :

js
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" ]

//      for of
//      matchAll 
matches = str.matchAll(regexp);

Array.from(matches, (m) => m[0]);
// Array [ "foo", "foo" ]

matchAll . match() /g:

js
var regexp = /t(e)(st(\d?))/g;
var str = "test1test2";

str.match(regexp);
// Array ['test1', 'test2']

matchAll :

js
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]

Specification
ECMAScript 2027 LanguageSpecification
# sec-string.prototype.matchall


Web Proxy Viewer  |  New URL  |  Original Page