โœจmatchAll() using named groups

JS โŸฉ primitives โŸฉ String โŸฉ methods โŸฉ .matchAll() โŸฉ using named groups

๐Ÿ’พ ็จ‹ๅผ๏ผšreplit

// โญ๏ธ pattern: yyyy-mm-dd (โญ๏ธ using "named groups")              
//          โ•ญ--- year ---โ•ฎ โ•ญ--- month ---โ•ฎ โ•ญ--- day ---โ•ฎ
let date = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g;

// text
let str = "2019-10-30, 2020-01-01, 18:24:00";

// โญ๏ธโญ๏ธโญ๏ธ result of `str.matchAll()` is an "iterable", NOT an arrayโ—๏ธ
let matches = str.matchAll(date);

for (let match of matches) {
    let { year, month, day } = match.groups;    // โญ๏ธ object destruturing
    console.log(`year: ${year}, month: ${month}, day: ${day}`);
}

// output
// -------------------------------
// year: 2019, month: 10, day: 30
// year: 2020, month: 01, day: 01

Last updated