• JavaScript Video Tutorials

JavaScript String match() Method



The JavaScript String match() method searches a string or a regular expression in the original string and returns an array with all matches. It returns Null if no match is found. If the search value is a string, it is converted into the regular expression and starts searching.

If the regular expression is passed to the match() method, and the 'g'(global) flag is used, all results matching the complete regular expression will be returned, but If the 'g' flag is not used, only the first complete match and its related capturing groups will be returned.

Syntax

Following is the syntax of JavaScript String match() method −

match(regexp)

Parameters

  • regexp − A regexp(regular expression) object.

Return value

This method returns an array with all matches.

Example 1

If the match is found in the original string, then it returns an array with matches.

In the following example, we are using the JavaScript String match() method to search for the string 'h' and retrieve an array with all matches in the original string "Hi how are you?"

<html>
<head>
<title>JavaScript String match() Method</title>
</head>
<body>
<script>
   const str = "Hi how are you?";
   let search_str = "h";
   document.write("Original string: ", str);
   document.write("<br>Search string: ", search_str);
   document.write("<br>An array with all matches: ", str.match(search_str));
</script>    
</body>
</html>

Output

The above program returns 'h'.

Original string: Hi how are you?
Search string: h
An array with all matches: h

Example 2

When we search for the regular expression '/src/g' globally.

The following is another example of the JavaScript match() method. Here, we use this method to search for the regular expression '/src/g' globally in the original string 'JavaScript is a scripting language' and try to retrieve an array with matches.

<html>
<head>
<title>JavaScript String match() Method</title>
</head>
<body>
<script>
   const str = "JavaScript is a scripting langauge";
   let regexp = /scr/g;
   document.write("Original string: ", str);
   document.write("<br>Regexp: ", regexp);
   document.write("<br>An array with all matches: ", str.match(regexp));
</script>    
</body>
</html>

Output

After executing the above program, it will return "src".

Original string: JavaScript is a scripting langauge
Regexp: /scr/g
An array with all matches: scr

Example 3

When we search for the regular expression '/src/gi' globally and case-insensitively.

In this program, we are using the JavaScript String search() method to search for the regular expression '/src/gi' globally and case-insensitively in the original string "JavaScript is a scripting language" and trying to retrieve an array with all matches.

<html>
<head>
<title>JavaScript String match() Method</title>
</head>
<body>
<script>
   const str = "JavaScript is a scripting langauge";
   let regexp = /scr/gi;
   document.write("Original string: ", str);
   document.write("<br>Regexp: ", regexp);
   document.write("<br>An array with all matches(global case-insensitive): ", str.match(regexp));
</script>    
</body>
</html>

Output

The above program returns "Src, src".

Original string: JavaScript is a scripting langauge
Regexp: /scr/gi
An array with all matches(global case-insensitive): Scr,scr

Example 4

If no match is found, the match() method will return null.

<html>
<head>
<title>JavaScript String match() Method</title>
</head>
<body>
<script>
   const str = "TutorialsPoint";
   let regexp = /tuto/g;
   document.write("Original string: ", str);
   document.write("<br>Regexp: ", regexp);
   document.write("<br>An array with all matches: ", str.match(regexp));
</script>    
</body>
</html>

Output

Once the above program is executed, it will return 'null'.

Original string: TutorialsPoint
Regexp: /tuto/g
An array with all matches: null
Advertisements