String.matchAll()


String.matchAll(), as the name suggests, returns all matches of the supplied pattern. The match() method takes a single parameter which is either a RegExp object or has a Symbol.match method. If it is neither, it is converted into a RegExp object using new RegExp(pattern). It returns an array each element of which contains the same information that String.match() returns for the single match without the ‘g’ flag. matchAll() requires the ‘g’ flag to be present.

Let us try the same example as the one for match(). We wan’t to find all substrings that are either ‘bin’ or ‘boo’ in the target string ‘bada bing, bada boom’. The regex to match either ‘bin’ or ‘boo’ would be ‘/bin|boo/’. We will add the ‘g’ flag because the matchAll() method requires it and for good measure we will also add the ‘d’ flag because we wan’t the indices for all the matches. String.matchAll() returns an array. Each element of the array is equivalent to the object returned by String.match() without the ‘g’ flag. The above regular expression when matched against ‘bada bing, bada boom’, returns a value that is an array containing two elements, one for the match ‘bin’ and one for ‘boo’ at indexes 5 and 16. As mentioned above, each element is equivalent to the single result returned by String.match() without the ‘g’ flag. So each element itself is an array. Each element has the following properties

  • The first array has an element at index zero whose value is ‘bin’. The values at index 1 and above are the text matched by capture groups if there are any in the regex (we will study capture groups later). The second array has its first element whose value is ‘boo’
  • Each element, which are arrays as mentioned above, has a named property index which gives the starting index of the match. In our example, the ‘index’ property of the first array has a value ‘5’ because ‘bin’ starts at index 5 in ‘bada bing, bada boom’. The second array has its ‘index’ property as ’16’ because ‘boo’ starts at index 16 in ‘bada bing, bada boom’
  • Each element has a named property ‘indices’ which is an array of arrays, each of which contains exactly 2 elements. The first array element is the starting index (inclusive) and the second is the ending index (exclusive) of the match for the regex. The first element corresponding to the matched string ‘bin’ has its ‘indices’ property a value of [[5, 8]] which means it’s an array of array with a single element which itself is an array of two elements. The values 5 and 8 indicate that matched text ‘bin’ starts at index 5 of the input string and ends at 7. The second element corresponding to the matched string ‘boo’ has its ‘indices’ property a value of [[16, 19]] because ‘boo’ starts at index 16 and ends at 18 in ‘bada bing, bada boom’.
  • If the regular expression has named capture groups, each element in the returned array will have a groups property which we will study later

Now let us look at the above example in code

let pattern = /bin|boo/dg;
let input = 'bada bing, bada boom';
//matches is an array which has an element
//for each match. In our case, our pattern
//matches twice, once for 'bin' and and once
//for 'boo at index 5 and 16 of the input
//string. So there are two elements in
//array
let matches = input.matchAll(pattern);
for (let match of matches) {
//match is an array whose first element
//is the matched text for the
//entire regex and subsequent elements
//are the matched texts for any capture
//groups that are present in the regex
for (text of match) {
console.log(text);
}
//Each element of the array also
//has an index named property which
//says where the match starts in the
//input string
let index = match.index;
console.log("index = " + index);
//Each match also has an 'indices' property
//which is an array of arrays. Each sub array
//has exactly two elemennts which tell
//where the matching text starts (including)
//and where it ends (excluding). There is one
//sub array for the text matched for the entire
//regex and one for matched text of each
//capture group
let indices = match.indices;
//Use destructuring for assign the values
//of each sub array to 'first' and 'last'
for (let [first, last] of indices) {
//log the first and last index
console.log("(" + first + "," + last + ")");
}
console.log("\n");
}