JavaScript | match()

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

string.match(regExp)

Parameters: Here the parameter is “regExp” i.e, regular expression which will compare with the given string.
Return Value: It will return an array that contain the matches one item for each match or if the match will not found then it will return Null.
JavaScript code to show the working of match() function:
Example 1:

Input: 
var string = Welcome to geeks for geeks!
document.write(string.match(/eek/g);
Output:
eek, eek

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



filter_none

edit
close

play_arrow

link
brightness_4
code

<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);
        document.write("Output : " + result);
    } matchString();
      
</script>                    

chevron_right


Output:

eek,eek

Example 2:

Input:
var string = "Welcome to GEEKS for geeks!";
document.write(string.match(/eek/i);
Output:
EEK

In the above example substring “eek” will match with given string and it will return instantly if it found the match. Here “i” parameter helps to find the case-sensitive match in the given string.
Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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);
        document.write("Output : " + result);
    } matchString();
      
</script>                    

chevron_right


Output:

EEK

Example 3:

Input:
var string = "Welcome to GEEKS for geeks!";
document.write(string.match(/eek/gi);
Output:
EEK, eek

In the above example substring “eek” will match with given string and it will return instantly if it found the match. Here “gi” parameter helps to find the case-sensitive match and all possible combination in the given string.
Code #3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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);
        document.write("Output : " + result);
    } matchString();
      
</script>                    

chevron_right


Output:

EEK,eek


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.