Table of contents
1.
Introduction
2.
Syntax
3.
Parameters
4.
Return Value
5.
Example 1: Using /g Flag
5.1.
JavaScript
6.
Example 2: Using /i Flag
6.1.
JavaScript
7.
Example 3: Using /gi Flag
7.1.
JavaScript
8.
Supported Browsers
9.
Frequently Asked Questions
9.1.
What is the difference between using the /g flag and not using it with the match() method?
9.2.
How does the match() method handle case sensitivity in JavaScript?
9.3.
What does the match() method return if there are no matches found in the string?
10.
Conclusion
Last Updated: Sep 13, 2024
Easy

JavaScript String match() Method

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaScript String match() Method is a powerful tool used to search for a match between a regular expression and a string. It returns an array of results when there is a match, or null if no match is found. This method is particularly useful for pattern matching and extracting specific parts of a string based on patterns.

JavaScript String match() Method

In this article, we will explore the JavaScript String match() Method in detail, discussing its syntax, parameters, return values, and how to use different flags such as /g, /i, and /gi. 

Syntax

The syntax for using the String match() Method is quite easy to understand. It takes a regular expression as its argument:

string.match(regexp);
  • string: The string in which you want to search for a pattern.
     
  • regexp: A regular expression object or literal that defines the pattern to search for within the string.

Parameters

The String match() Method primarily accepts one parameter:

  • regexp: This is the regular expression that specifies the pattern you want to search for. Regular expressions are powerful tools for pattern matching, allowing you to search for specific strings, sets of characters, or even more complex patterns.

Return Value

The String match() Method returns different results depending on the presence of the global (/g) flag in the regular expression:

  • If the /g flag is used: The method returns an array of all matches found in the string. If no matches are found, it returns null.
     
  • If the /g flag is not used: The method returns an array with the first match found and additional details about the match, such as the index at which the match was found and the original input string.

Example 1: Using /g Flag

The /g flag stands for "global." When you use this flag, the regular expression doesn't stop after finding the first match. Instead, it continues to search through the entire string, returning all possible matches found.

Let's see an example:

  • JavaScript

JavaScript

let text = "Coding Ninjas are the best Ninjas";

let result = text.match(/Ninjas/g);

console.log(result);
You can also try this code with Online Javascript Compiler
Run Code

 

Output

["Ninjas", "Ninjas"]


Explanation:
In this example, the /g flag ensures that all occurrences of the word "Ninjas" are matched. The match() method returns an array containing both instances of the word "Ninjas" found in the string.

Example 2: Using /i Flag

The /i flag stands for "case-insensitive", when you use this flag, the regular expression ignores case differences between the pattern and the string. This means it will match both uppercase and lowercase versions of the string Let's see an example:

  • JavaScript

JavaScript

let text = "Coding Ninjas are the best ninjas";

let result = text.match(/Ninjas/i);

console.log(result);
You can also try this code with Online Javascript Compiler
Run Code


Output

["Ninjas"]


Explanation
The /i flag makes the search case-insensitive. Although the word "ninjas" in the string is in lowercase, the match() method identifies a match since it ignores case sensitivity.

Example 3: Using /gi Flag

The /gi flag combines both the global (/g) and case-insensitive (/i) flags. This means the regular expression will find all matches in the string, regardless of the case. Let's see an example:

  • JavaScript

JavaScript

let text = "Coding Ninjas are the best ninjas";

let result = text.match(/Ninjas/gi);

console.log(result);
You can also try this code with Online Javascript Compiler
Run Code


Output

["Ninjas", "ninjas"]


Explanation
In this example, the /gi flags are used to search for all occurrences of the word "Ninjas" in the string, ignoring case sensitivity. The match() method retrieves both uppercase and lowercase versions of the word.

Supported Browsers

The method is widely supported across all modern browsers. Here’s a list of browsers where this method is fully functional:

  • Google Chrome
     
  • Mozilla Firefox
     
  • Microsoft Edge
     
  • Apple Safari
     
  • Opera
     
  • Internet Explorer 11

Frequently Asked Questions

What is the difference between using the /g flag and not using it with the match() method?

When you use the /g flag with the match() method, it returns an array of all occurrences of the pattern in the string. Without the /g flag, it returns an array with just the first match and additional details like the index and the input string.

How does the match() method handle case sensitivity in JavaScript?

By default, the match() method is case-sensitive. However, you can make it case-insensitive by using the /i flag in your regular expression, which allows the method to match patterns regardless of their case (uppercase or lowercase).

What does the match() method return if there are no matches found in the string?

If no matches are found and the /g flag is used, the match() method returns null. Without the /g flag, if no match is found, it will also return null.

Conclusion

In this article, we disucssed the JavaScript String match() method, a useful tool for pattern matching in strings. We covered its syntax, parameters, return values, and how to apply flags like /g, /i, and /gi to modify search behavior. With practical examples, we demonstrated how match() helps identify patterns in strings, making it a valuable function for JavaScript developers.

You can also check out our other blogs on Code360.

Live masterclass