How To Use JavaScript Match Method

JavaScript match method is a function that takes a parameter(e.g, string) and matches it against an expression.

The expression can be a string.

When you invoke the JavaScript match method, it searches through the string for a match, and when it finds it, it returns;

1. an array of the match

2. it’s index value.

One good feature of JavaScript match method is that, the text does not need to be exactly the same as the match for it to return true and there is no better way to make you understand than using an examples.

Example on how to use JavaScript match method

lets take the following examples to try out how to use the match method
assuming we want to search for “ldo” in the string text below

JavaScript
let commentary = 'the football players who scored in todays match are ronaldo and rivaldo';
//search target is 'ldo'
let result = commentary.match('ldo');
console.log(result);

the result of the search is shown in the picture below

you can clearly see that the it returns an array and even though there is no exact text with “ldo”
Notice that there are two places where the search value (ldo) is found (ronaldo, rivaldo).

But it only returns the first match and ignore the other one.

In order to search for all possible match, we search for the the match globally by adding a ‘g’.

This is shown below

JavaScript

let commentary = 'the football players who scored in todays match are ronaldo and rivaldo';
//search target is 'ldo'
let result = commentary.match(/ldo/g);
console.log(result);

Result

Now you can see what the array returned from the match contains two item. You can check how to use JavaScript map method here

These two items are matches found by searching for the match globally using regular expression.
while this is nice, if you have a situation where one of the match is in different case like uppercase, you would notice that if you search for the matches it will return just one match as seen below

JavaScript

 let commentary = 'the football players who scored in todays match are ronaldo and rivaLDO';
//search target is 'ldo'
let result = commentary.match(/ldo/g);
console.log(result);

Result

Why this happens is because the match method is searches for all possible matches but its case sensitive. so ensure that the match method returns all possible occurrence of the match, we use a combination of the global regular expression(g) with an “i”. this is shown below

javaScript
 let result = commentary.match(/ldo/gi);
//with this, it will return all matches both in uppercase and lowercase

Result

And there you go. the JavaScript match method run against the commentary expression returns an array of two match.
you are not limited to these examples alone, as there might be other things you can do with the JavaScript match method, but these examples are just to give you an understanding of how the JavaScript match method works.

Leave a Comment

Your email address will not be published. Required fields are marked *