JavaScript

What is the match() method in JavaScript

The match method is an inbuilt JavaScript method that searches a string against a regular expression. The regular expression is the value we want to search for. If the searching match is found, it returns an array object containing the regular expression. If the search match is not found, it returns a null value

In this post we will have an in-depth discussion about the match() method; We will start off by looking at the syntax of the match() method:

Syntax:

string.match(regExp);

String is the variable in which we are searching and myexpression is the expression we are searching for.

How does the match() method work

Now, let’s look at a few examples to better understand the JavaScript match method:

Note: The browser console is used for the demonstration of examples in this article.

var str = "Hello and Welcome to Linux Hint!";
console.log(str.match('Welcome'));

The match method returns only the first occurrence of the regular expression inside the string. If we need to check for multiple occurrences then we will need to add the g modifier. The g modifier is a global search modifier that searches the expression at all instances. This returns all the matched elements. If we don’t put this modifier then only the first match will be returned:

var str = "Hello and Welcome to Linux Hint!";
console.log(str.match(/el/g));

We can also use the i modifier with the match method as well. This performs case insensitive matching. In simple words, the match method by default performs a case sensitive search but when we use the i modifier it ignores case sensitivity.

var str = "Hello and Welcome to Linux Hint!";
console.log(str.match(/linux/i));

The match method returns null if no match is found:

var str = "Hello and Welcome to Linux Hint!";
console.log(str.match('my'));

If we do not give any parameter the match method then it returns the array object containing an empty string:

var str = "Hello and Welcome to Linux Hint!";
console.log(str.match());

Using special characters in regExp

We can use different special characters in our regular expressions to carry out more complex searches. The \d sign can be used to search for the first digit in the regular expression:

var str = "abcdefghjkl123456789";
console.log(str.match(/\d/));

If we want to get all of the digits present in the string:

var str = "abcdefghjkl123456789";
console.log(str.match(/\d/g));

The \D sign will output an array of object that contains every character that the /d sign ignores:

var str = "abcdefghjkl123456789";
console.log(str.match(/\D/g));

A character followed by the $ sign can be used to check whether that is the last character of the given string:

var str = "Hello and Welcome to Linux Hint!";
console.log(str.match(/!$/));

Difference between the match and search methods

The search method searches for the given parameter, if found, it returns the index of that expression in the string whereas the match method returns the expression inside an array object:

var str = "Hello and Welcome to Linux Hint!";
console.log('match() method: ' + str.match('and'));
console.log('search() method: ' + str.search('and'));

If the match is not found then the search method returns -1 whereas the match() method returns null:

var str = "Hello and Welcome to Linux Hint!";
console.log(str.match('near'));
console.log(str.search('near'));

Note: You can put your JavaScript code in a script tag and then run your html file in the browser or just create a different file with js extension and reference it inside the html file using the script tag.

Conclusion

The match() method is used to find a regular expression (regExp) in a string. The match method returns an array object containing the regular expression, its index, group and the original string. In this how-to guide we learned how to use the match() method in JavaScript to search for a regular expression inside a string. Then we learned to use special characters and modifiers with the match() method to perform more complex searches. Moreover, we also discussed the difference between the search() and match() method in JavaScript.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.