The ‘d‘ flag instructs the regex engine to generate indices for matches for the entire regular expression and any capture groups present in the regular expression. The indices property is an array or arrays where each sub array contains the first and last index of the matched text.
The array at index ‘0’ contains the first and last index of the entire match. The arrays at subsequent indices contain the first and last indices of any capturing groups.
The indices.groups is a named property which is a null-prototype object whose
- properties are names of the named capturing groups
- values are two element arrays where the first element is the start index and the second element is the end index of the matching substring for this named capturing group
The below code finds the indices of the capture groups and named capture groups. For more information look at String.match() and String.matchAl().
/* This pattern will extract user handles from a string. There are named capture groups for the userid, subdomain and top level domain*/let wordPttn = /(?<username>[\w]+)@(?<subdomain>[[\w]--[_]][[\w\-]--[_]]{0,63})\.(?<TLD>[[\w]--[_]][[\w\-]--[_]]{1,63})/gdv; let targetString = "Sample user handles : mark@x.com, ad-hir@google.com, adhir@gmail.com, admin-999@primacy.com"; let results = targetString.matchAll(wordPttn);let matchNum = 1;for (let result of results) { console.log(`---------Match ${matchNum}--------------`); let indices = result.indices; for (let i = 0; i < indices.length; i++) { let [start, end] = indices[i]; let matchMsg = (i === 0 ? "patternMatch" : "capture group " + i); console.log(`indices = [${start}, ${end}], ${matchMsg} = ${targetString.substring(start, end)}`); } let groups = indices.groups; for (let [name, nmedGrpInds] of Object.entries(groups)) { let namedGrpIndices = groups[name]; console.log(`named capture group = ${name}, indices = [${nmedGrpInds[0]}, ${nmedGrpInds[1]}], matched string = ${targetString.substring(nmedGrpInds[0], nmedGrpInds[1])}`); } matchNum ++; console.log("\n");}
Output:
---------Match 1--------------
indices = [22, 32], patternMatch = mark@x.com
indices = [22, 26], capture group 1 = mark
indices = [27, 28], capture group 2 = x
indices = [29, 32], capture group 3 = com
named capture group = username, indices = [22, 26], matched string = mark
named capture group = subdomain, indices = [27, 28], matched string = x
named capture group = TLD, indices = [29, 32], matched string = com
---------Match 2--------------
indices = [37, 51], patternMatch = hir@google.com
indices = [37, 40], capture group 1 = hir
indices = [41, 47], capture group 2 = google
indices = [48, 51], capture group 3 = com
named capture group = username, indices = [37, 40], matched string = hir
named capture group = subdomain, indices = [41, 47], matched string = google
named capture group = TLD, indices = [48, 51], matched string = com
---------Match 3--------------
indices = [53, 68], patternMatch = adhir@gmail.com
indices = [53, 58], capture group 1 = adhir
indices = [59, 64], capture group 2 = gmail
indices = [65, 68], capture group 3 = com
named capture group = username, indices = [53, 58], matched string = adhir
named capture group = subdomain, indices = [59, 64], matched string = gmail
named capture group = TLD, indices = [65, 68], matched string = com
---------Match 4--------------
indices = [76, 91], patternMatch = 999@primacy.com
indices = [76, 79], capture group 1 = 999
indices = [80, 87], capture group 2 = primacy
indices = [88, 91], capture group 3 = com
named capture group = username, indices = [76, 79], matched string = 999
named capture group = subdomain, indices = [80, 87], matched string = primacy
named capture group = TLD, indices = [88, 91], matched string = com
Here is another example to find the indices for all the groups and named capture groups for non Latin script. So you will have to use the unicode character class \p{}.
let wordPttn = /(?<word>\p{L}+)/dgv; let targetString = `Oṃ Saha Nāvavatu Saha Nau Bhunaktu Saha Vīryaṃ Karavāvahai Tejasvi Nāvadhītam Astu Mā Vidviṣāvahai Oṃ Śāntiḥ Śāntiḥ Śāntiḥ`;let results = targetString.matchAll(wordPttn);let matchNum = 1;for (let result of results) { console.log(`---------Match ${matchNum}--------------`); let indices = result.indices; for (let i = 0; i < indices.length; i++) { let [start, end] = indices[i]; let matchMsg = (i === 0 ? "patternMatch" : "capture group " + i); console.log(`indices = [${start}, ${end}], ${matchMsg} = ${targetString.substring(start, end)}`); } let groups = indices.groups; for (let [name, nmedGrpInds] of Object.entries(groups)) { let namedGrpIndices = groups[name]; console.log(`named capture group = ${name}, indices = [${nmedGrpInds[0]}, ${nmedGrpInds[1]}], matched string = ${targetString.substring(nmedGrpInds[0], nmedGrpInds[1])}`); } matchNum ++; console.log("\n");}
Output:
---------Match 1--------------
indices = [0, 2], patternMatch = Oṃ
indices = [0, 2], capture group 1 = Oṃ
named capture group = word, indices = [0, 2], matched string = Oṃ
---------Match 2--------------
indices = [3, 7], patternMatch = Saha
indices = [3, 7], capture group 1 = Saha
named capture group = word, indices = [3, 7], matched string = Saha
---------Match 3--------------
indices = [8, 16], patternMatch = Nāvavatu
indices = [8, 16], capture group 1 = Nāvavatu
named capture group = word, indices = [8, 16], matched string = Nāvavatu
---------Match 4--------------
indices = [23, 27], patternMatch = Saha
indices = [23, 27], capture group 1 = Saha
named capture group = word, indices = [23, 27], matched string = Saha
---------Match 5--------------
indices = [28, 31], patternMatch = Nau
indices = [28, 31], capture group 1 = Nau
named capture group = word, indices = [28, 31], matched string = Nau
---------Match 6--------------
indices = [32, 40], patternMatch = Bhunaktu
indices = [32, 40], capture group 1 = Bhunaktu
named capture group = word, indices = [32, 40], matched string = Bhunaktu
---------Match 7--------------
indices = [47, 51], patternMatch = Saha
indices = [47, 51], capture group 1 = Saha
named capture group = word, indices = [47, 51], matched string = Saha
---------Match 8--------------
indices = [52, 58], patternMatch = Vīryaṃ
indices = [52, 58], capture group 1 = Vīryaṃ
named capture group = word, indices = [52, 58], matched string = Vīryaṃ
---------Match 9--------------
indices = [59, 70], patternMatch = Karavāvahai
indices = [59, 70], capture group 1 = Karavāvahai
named capture group = word, indices = [59, 70], matched string = Karavāvahai
---------Match 10--------------
indices = [77, 84], patternMatch = Tejasvi
indices = [77, 84], capture group 1 = Tejasvi
named capture group = word, indices = [77, 84], matched string = Tejasvi
---------Match 11--------------
indices = [85, 95], patternMatch = Nāvadhītam
indices = [85, 95], capture group 1 = Nāvadhītam
named capture group = word, indices = [85, 95], matched string = Nāvadhītam
---------Match 12--------------
indices = [96, 100], patternMatch = Astu
indices = [96, 100], capture group 1 = Astu
named capture group = word, indices = [96, 100], matched string = Astu
---------Match 13--------------
indices = [101, 103], patternMatch = Mā
indices = [101, 103], capture group 1 = Mā
named capture group = word, indices = [101, 103], matched string = Mā
---------Match 14--------------
indices = [104, 116], patternMatch = Vidviṣāvahai
indices = [104, 116], capture group 1 = Vidviṣāvahai
named capture group = word, indices = [104, 116], matched string = Vidviṣāvahai
---------Match 15--------------
indices = [123, 125], patternMatch = Oṃ
indices = [123, 125], capture group 1 = Oṃ
named capture group = word, indices = [123, 125], matched string = Oṃ
---------Match 16--------------
indices = [126, 132], patternMatch = Śāntiḥ
indices = [126, 132], capture group 1 = Śāntiḥ
named capture group = word, indices = [126, 132], matched string = Śāntiḥ
---------Match 17--------------
indices = [133, 139], patternMatch = Śāntiḥ
indices = [133, 139], capture group 1 = Śāntiḥ
named capture group = word, indices = [133, 139], matched string = Śāntiḥ
---------Match 18--------------
indices = [140, 146], patternMatch = Śāntiḥ
indices = [140, 146], capture group 1 = Śāntiḥ
named capture group = word, indices = [140, 146], matched string = Śāntiḥ