# quick brown fox

[JS](../../../..) ⟩ [value](../../..) ⟩ [object](../../../obj) ⟩ [regex](..) ⟩ [example]() ⟩ quick brown fox

{% hint style="success" %}
use [**matchAll()**](../../../prim/str/method/str.matchall) with [<mark style="color:yellow;">**`/d`**</mark>](../flag/d), `/g`, `/i` flags.
{% endhint %}

{% tabs %}
{% tab title="💈範例" %}
* replit ⟩ [quick brown fox](https://replit.com/@pegasusroe/regex-quick-brown-fox#index.js)

```javascript
// ⭐ 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));
```
{% endtab %}

{% tab title="👥 相關" %}
* [flag](../flag "mention")
* [str.matchall](../../../prim/str/method/str.matchall "mention")
{% endtab %}

{% tab title="📗 參考" %}
* [ ] Eloquent JS ⟩ [Regular Expressions](https://eloquentjavascript.net/09_regexp.html)
{% endtab %}

{% tab title="📘 手冊" %}
* [str.matchAll(regex)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll)
* [str.match(regex)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)
{% endtab %}
{% endtabs %}
