JavaScript string.match() method

In this tute, we will discuss the JavaScript string.match() method.

The JavaScript match() is an in-built function used to search for a given string in the original string. The match() method needs a string in which the search is to be carried out and a regular expression that is to be matched.

When the match() method is called, it returns either an array of values or null.

Syntax:

str.match(regularExpression)

Key points:

  • Firstly, if the string is matched, it returns an array of matched strings.
  • When no substring is matched, it returns null.
  • When a substring is matched, characters in it are not chosen for the next matching.

string.match(regularExpresssion):

Based on the regular expression passed, the match() method returns objects accordingly.

The three modifies used in the regular expression here are:

  • g – for a global match
  • i – for case-insensitive matching
  • m – for multiline matching

 

Example 1:

In this example, we will use the ‘g‘ modifier for global matching. When this modifier is used, all the distinctly matched strings are returned as an array. In case the regularExpression is not found in the string, null is returned.

When this modifier is used, case-sensitive matching is done.

Snippet:

var str = "Hahahahaha";

var res = str.match(/haha/g);
document.write(res);

Ouput:

haha,haha

Example 2:

In this example, we will use the ‘m‘ modifier for matching. When this modifier is used, the first matched string is returned. In case the regularExpression is not found in the string, null is returned.

When this modifier is used, case-sensitive matching is done.

Snippet:

var str = "Hahahahaha";

var res = str.match(/haha/m);
document.write(res);

Ouput:

haha

Example 3:

In this example, we will use the ‘i‘ modifier for case-insensitive matching. When this modifier is used, the first matched string is returned. In case the regularExpression is not found in the string, null is returned.

When this modifier is used, case-insensitive matching is done.

Snippet:

var str = "Hahahahaha";

var res = str.match(/haha/i);
document.write(res);

Ouput:

Haha

Example 4:

In this example, we will use the ‘ig‘ modifiers for case-insensitive global matching. Similar to example 1, when this modifier is used, all the distinctly matched strings are returned as an array. In case the regularExpression is not found in the string, null is returned.

When this modifier is used, case-insensitive matching is done.

Snippet:

var str = "Hahahahaha";

var res = str.match(/haha/ig);
document.write(res);

Ouput:

Haha,haha

 

Furthermore, to get a clearer view:

Modifier Case-sensitivity Return value
g sensitive Array of matches
m sensitive First match
i insensitive First match
ig insensitive Array of matches
var str    = "Hahahahaha";

var res-g  = str.match(/haha/g);  // [haha, haha]

var res-m  = str.match(/haha/m);  // haha

var res-i  = str.match(/haha/i);  // Haha

var res-ig = str.match(/haha/m);  // [Haha, haha]

 

Finally, If you have any queries or doubts related to JavaScript string.match() method, simply comment in the comment section provided below. Whenever there’s a doubt, it’s good to throw in a forum.

Also, read:

Leave a Reply

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