The Wayback Machine - https://web.archive.org/web/20230512160218/https://www.geeksforgeeks.org/javascript-match-function/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript String match() Function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The JavaScript String match() Function is an inbuilt function in JavaScript used to search a string for a match against any regular expression. If the match is found, then this will return the match as an array. 
Syntax: 

string.match(regExp)

Parameters: This function accepts a single parameter.

  • regExp: (i.e. regular expression) which will compare with the given string. 

Return Value: It will return an array that contains the matches one item for each match or if the match is not found then it will return Null. 

JavaScript code to show the working of match() function: 

Example 1: In the example, substring “eek” will match with the given string, and when a match is found, it will return an array of string objects. Here “g” flag indicates that the regular expression should be tested against all possible matches in a string. 

javascript




<script>
    // initializing function to demonstrate match()
    // method with "g" para
    function matchString() {
        var string = "Welcome to geeks for geeks";
        var result = string.match(/eek/g);
        console.log("Output : " + result);
    } matchString(); 
</script>

Output: 

eek,eek

Example 2: In this example, the substring “eek” will match with the given string, and it will return instantly if it found the match. Here “i” parameter helps to find the case-insensitive match in the given string. 

javascript




<script>
    // initializing function to demonstrate match()
    // method with "i" para
    function matchString() {
        var string = "Welcome to GEEKS for geeks!";
        var result = string.match(/eek/i);
        console.log("Output : " + result);
    } matchString();
      
</script>

Output: 

EEK

Example 3: In this example, the substring “eek” will match with the given string, and it will return instantly if it found the match. Here “gi” parameter helps to find the case-insensitive match AND all possible combinations in the given string. 

javascript




<script>
    // initializing function to demonstrate match()
    // method with "gi" para
    function matchString() {
        var string = "Welcome to GEEKS for geeks!";
        var result = string.match(/eek/gi);
        console.log("Output : " + result);
    } matchString();   
</script>

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 Browser:

  • chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 4 and above
  • Safari 1 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


My Personal Notes arrow_drop_up
Last Updated : 30 Nov, 2022
Like Article
Save Article
Similar Reads