JavaScript match() Function - GeeksforGeeks

06 Oct, 2021
The string.match() 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: Here the parameter is “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 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 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. code #1:  
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);        document.write("Output : " + result);    } matchString();     </script>                   
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, 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. Code #2:  
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);        document.write("Output : " + result);    } matchString();     </script>                   
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, 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. Code #3:  
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);        document.write("Output : " + result);    } matchString();     </script>                   
Output:  
EEK,eek
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 
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.
ManasChhabra2
impress
ysachin2314
javascript-functions
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Difference Between PUT and PATCH Request
JavaScript | console.log() with Examples
How to read a local text file using JavaScript?
Node.js | fs.writeFileSync() Method