Before we dive full fledged into the various features of regular expressions, we will first look at a couple of methods in the JavaScript API that work with regular expressions, namely String.match() and String.matchAll() so that we can practice what we learn.
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 whose items depend on whether the global flag ‘g’ is set or not.
If the ‘g’ flag is not set it, the returned array contains the first match, information about the capturing groups in it, the starting index of the match, the input string and if the d flag is set, the indices of the match. We will revisit the match() method after we have learnt about capturing groups.
If the ‘g’ flag is set, it will return an array of all matches. For example if we are searching for the pattern ‘ba’ in ‘bada bing, bada boom’, it will return an array of two strings – ‘ba’ and ‘ba’
/* Create a pattern that matches
the string 'ba' with flag 'g'*/
let wordPttn = /ba/g;
//The input is the string in which
//the pattern is to be found
let targetString = "bada bing, bada boom";
//String.match() returns an array of matched strings
let results = targetString.match(wordPttn);
for (let result of results) {
console.log(result);
}
Result
baba
Let’s try the same without the ‘g’ flag. String.match() without the ‘g’ flag returns the first match and a bunch of information about the match. The returned array contains the matched text for the entire regular expression and the matched text for each of the capture groups. We don’t have any capture groups now, so our array will contain just one element.
index property
JavaScript arrays are fundamentally objects. So they can have named properties or key, value pairs. The named property index gives the starting index of the match.
/* Create a pattern that matches the string 'ba'without the 'g' flag.*/let wordPttn = /ba/; //The input is the string in which//the pattern is to be foundlet targetString = "bada bing, bada boom";//String.match() without the 'g' flag//returns the first match and a bunch of//information about the matchlet results = targetString.match(wordPttn);/* The returned array contains the matched text for the entire regular expression and the matched text for each of the capture groups. . We don't have any capture groups now, so our array will contain just one element*/ for (let result of results) { console.log("Matching Text = " + result); }/* JavaScript arrays are fundamentally objects. So they can have named properties or key, value pairs*//* The named property index gives the starting index of the match*/console.log("Starting Index = " + results.index);
The output will be
Matching Text = baStarting Index = 0
indices property
When the ‘d’ flag is set, the returned array from ‘String.match() has the named property ‘indices’. The named property indices 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. Subsequent elements contain starting and ending indices for capture group matches. For example the regex /bin|boo/ matches either the string ‘bin’ or ‘boo’ (We will study alternation in a bit). When matched against ‘bada bing, bada boom’, the returned value is an array whose element at index zero has the value ‘bin’. It’s indices property has 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 starts at index 5 of the input string and ends at 7. Now let’s look at an example on how to use the indices property.
/* Create a pattern that matches either the string 'bin' or 'boo' with the 'd' flag.*/let wordPttn = /bin|boo/d; //The input is the string in which//the pattern is to be foundlet targetString = "bada bing, bada boom";//String.match() without the 'g' flag//returns the first match and a bunch of//information about the matchlet match = targetString.match(wordPttn);/* The returned array contains the matched text for the entire regular expression and the matched text for each of the capture groups. . We don't have any capture groups now, so our array will contain just one element*/ for (let item of match) { console.log("Matching Text = " + item); }/* JavaScript arrays are fundamentally objects. So they can have named properties or key, value pairs*//* The named property index gives the starting index of the match*/console.log("Starting Index = " + match.index);/* The named property indices is an array of arrays, each of which contains exactly 2 elements. The first array element contains the starting index(inclusive) and ending index(exclusive) of the match for the regex. Subsequent elements contain starting and ending indices for capture group matches. For example to regex /bin|boo/ when matched against 'bada bing, bada boom', returns an object whose indices property has a value of [[5, 8]] match.indices = [[5, 8]] */ let indices = match.indices;for (let [start, end] of indices) { console.log("start = " + start + ", end = " + end);}
If the pattern has named groups, the returned object from match() also has a ‘groups’ property which gives information about the named groups. We will explore capture groups and the ‘groups’ property in more detail later.