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
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
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
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.