โœจquick brown fox

JS โŸฉ value โŸฉ object โŸฉ regex โŸฉ example โŸฉ quick brown fox

use matchAll() with /d, /g, /i flags.

// โญ custom pattern
// ------------------
// โ€ข match "quick brown" followed by "jumps", ignoring characters in between.
// โ€ข remember "brown" and "jumps" (groups)
// โ€ข flags: 
//   โ€ข /i - ignore case
//   โ€ข /g - global search
//   โ€ข /d - generate indices โญ 
//                    โ•ญโ”€โ”€โ”€โ”€ G1 โ”€โ”€โ”€โ”€โ”€โ•ฎ   โ•ญโ”€G2โ”€โ”€โ•ฎ
const regex = /quick\s(?<color>brown).+?(jumps)/dgi;
//                                   โ•ฐโ”€โ•ฏ any characters (?: as short as possible, lazy)

// index:   |01234567890123456789012345
//          |    โ•ญโ”€โ”€โ”€โ”€โ”€โ”€ match โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
const str = "The Quick Brown Fox Jumps Over The Lazy Dog";
// groups:             โ•ฐโ”€1โ”€โ•ฏ     โ•ฐโ”€2โ”€โ•ฏ

const matches = [...str.matchAll(regex)];    // iterator of matches -> array of matches
// 1. each match is an array โญ๏ธ 
// 2. match format: 
//                          โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€ match โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ  โ•ญโ”€G1โ”€โ”€โ•ฎ  โ•ญโ”€G2โ”€โ”€โ•ฎ
//    [match, ...groups] = ['Quick Brown Fox Jumps', 'Brown', 'Jumps']
//
// 3. additional properties:
// 
//    โ€ข index: 4                                       // index for match
//    โ€ข input: "The Quick Brown Fox ... Lazy Dog"      // original string
//    โ€ข groups: { color: 'Brown' }                     // named groups
//    โ€ข indices: [ [4, 25], [10, 15], [20, 25] ]       // indices for match/groups
//        โ€ข groups: { color: [10, 15] }                // indices for named groups

// โญ๏ธ pattern matching without generating indices
const regexWithoutGeneratingIndices = /quick\s(?<color>brown).+?(jumps)/gi;
const matches2 = [...str.matchAll(regexWithoutGeneratingIndices)];

;[
    // โญ๏ธ with indices
    matches[0],                          // first match
    // [
    //   'Quick Brown Fox Jumps',        // the "match" (substring)
    //   'Brown',                        // capture group #1
    //   'Jumps',                        // capture group #2
    //
    //   index: 4,                       // index for the "match"
    //   input: 'The Quick Brown ...',   // original string
    //   groups: { color: 'Brown' },     // named groups
    //
    //   indices: [                      // indices for match/groups โญ๏ธ 
    //     [ 4, 25 ],
    //     [ 10, 15 ],
    //     [ 20, 25 ],
    //     groups: { color: [ 10, 15 ] } // indices for named groups
    //   ]
    // ]
    
    matches[0][0],                 // the "match": 'Quick Brown Fox Jumps'
    matches[0].groups,             // named groups: { color: 'Brown' }
    matches[0].indices,            // indices for match/groups: [[4,25], [10,15], [20,25]]
    matches[0].indices.groups,     // indices for named groups: { color: [ 10, 15 ] }

    // โญ๏ธ without generating indices
    matches2[0],                          // first match
    // [
    //   'Quick Brown Fox Jumps',
    //   'Brown',
    //   'Jumps',
    //
    //   index: 4,
    //   input: 'The Quick Brown Fox Jumps Over The Lazy Dog',
    //   groups: { color: 'Brown' }
    // ]

].forEach(x => console.log(x));

Last updated