The Wayback Machine - https://web.archive.org/web/20250121092019/https://www.geeksforgeeks.org/javascript-string-match-method/
Open In App

JavaScript String match() Method

Last Updated : 10 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The match() method in JavaScript is used for identifying and retrieving substrings that fit a specified pattern, defined by a regular expression. It is often used when you need to find particular patterns within strings, enabling efficient text processing.

This method returns an array of matched substrings or null if no match is found.

Syntax:

string.match(regExp);

Parameters:

  • string: The string to be searched for a specific pattern.
  • regexp: A regular expression object or pattern string used to search the string.

Note: If the g flag is not used with the regular expression, only the first match will be returned. If the g flag is used, all matches will be returned.

Return Value:

  • Array: An array of matched substrings if matches are found.
  • null: If no match is found, the method returns null.

Example 1: Using the /g Flag (Global)

The global (g) flag ensures that all occurrences of the matching pattern are returned. Below is an example that matches the substring “eek” multiple times in the string:

javascript
// Initializing function to demonstrate match()
// method with "g" para
function matchString() {
    let string = "Welcome to geeks for geeks";
    let result = string.match(/eek/g);
    console.log("Output : " + result);
} matchString(); 

Output
Output : eek,eek

Example 2: Using the /i Flag (Case-Insensitive)

The case-insensitive (i) flag allows the match() method to ignore letter case when matching patterns. Here is an example:

javascript
// Initializing function to demonstrate match()
// method with "i" para
function matchString() {
    let string = "Welcome to GEEKS for geeks!";
    let result = string.match(/eek/i);
    console.log("Output : " + result);
} matchString();

Output
Output : EEK

Example 3: Using the /gi Flag (Global and Case-Insensitive)

When both the global (g) and case-insensitive (i) flags are used together, the match() method will find all case-insensitive matches throughout the string. Below is an example:

javascript
// Initializing function to demonstrate match()
// method with "gi" para
function matchString() {
    let string = "Welcome to GEEKS for geeks!";
    let result = string.match(/eek/gi);
    console.log("Output : " + result);
} matchString();   

Output
Output : EEK,eek

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Supported Browsers

JavaScript String match() Method- FAQs

What does the JavaScript match() method do?

The match() method in JavaScript checks if a string fits a pattern defined by a regular expression. It returns an array of matches or null if no match is found. This method is useful for finding specific patterns within strings.

How does the g flag affect the match() method?

The g (global) flag in the match() method ensures that the regular expression is tested against all possible matches in the string, rather than stopping after the first match.

What is the purpose of the i flag in the match() method?

The i (insensitive) flag in the match() method allows for case-insensitive matching. This means the method will match patterns regardless of whether the characters are in upper or lower case. Using the i flag, match() will find matches without considering the case of the characters.

Can the match() method use multiple flags simultaneously?

Yes, the match() method can use multiple flags simultaneously.

What will the match() method return if no matches are found?

If the match() method does not find any matches, it will return null. When no part of the string matches the regular expression pattern, match() returns null instead of an array.



Next Article

Similar Reads

three90RightbarBannerImg