Baseline Widely available
ÐеÑод 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"]
СинÑакÑÐ¸Ñ ÐаÑамеÑÑÑ
regexp
ÐбÑÐµÐºÑ ÑегÑлÑÑного вÑÑажениÑ. ÐÑли пеÑедано знаÑение, не ÑвлÑÑÑееÑÑ Ð¾Ð±ÑекÑом ÑегÑлÑÑного вÑÑажениÑ, оно неÑвно пÑеобÑазÑеÑÑÑ Ð² RegExp
иÑполÑзÑÑ new RegExp(obj)
.
ÐозвÑаÑаеÑÑÑ iterator (не пеÑезапÑÑкаемÑй).
ÐÑимеÑÑ Regexp.exec() и matchAll()Ðо Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ð¸Ñ Ð¼ÐµÑода matchAll
в JavaScript, можно бÑло иÑполÑзоваÑÑ Ð¼ÐµÑод regexp.exec (и ÑегÑлÑÑнÑе вÑÑÐ°Ð¶ÐµÐ½Ð¸Ñ Ñ Ñлагом /g
) в Ñикле Ð´Ð»Ñ Ð¿Ð¾Ð»ÑÑÐµÐ½Ð¸Ñ Ð´Ð¾ÑÑÑпа к ÑовпадениÑм:
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()
:
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
:
var regexp = /t(e)(st(\d?))/g;
var str = "test1test2";
str.match(regexp);
// Array ['test1', 'test2']
С matchAll
Ñ Ð²Ð°Ñ Ð¿Ð¾ÑвлÑеÑÑÑ Ðº ним доÑÑÑп:
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]
СпеÑиÑикаÑии СовмеÑÑимоÑÑÑ Ñ Ð±ÑаÑзеÑами СмоÑÑиÑе Ñакже
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