JavaScript String matchAll() Method



The JavaScript String matchAll() method retrieves an iterator of all the results that match a specified regular expression (regex) in the current string. However, if the specified regular expression does not find any matches in the current string, it will return an empty iterator.

If the regex does not contain the "g" flag or has not set the global (g) flag, it will throw a "TypeError" exception.

Syntax

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

matchAll(regexp)

Parameters

This method accepts a parameter named 'regexp', which is described below −

  • regexp − It is a regular expression object.

Return value

This method returns an iterator of matches in the current string, or an empty iterator if no matches are found.

Example 1

If the specified regular expression matches in this string, it will return an iterator containing all results that match in this string.

In the following program, we are using the JavaScript String matchAll() method to retrieve an iterator of all results that matches this string "TutorialsPointTuto" against the specified regular expression /Tuto/g.

<html>
<head>
<title>JavaScript String matchAll() Method</title>
</head>
<body>
<script>
   const str = "TutorialsPointTuto";
   document.write("String: ", str);
   const regex =/Tuto/g;
   document.write("<br>Regular expression: ", regex);
   //using the matchAll() function
   const iterator_array = [...str.matchAll(regex)];
   try {
      document.write("<br>An iterator: ", iterator_array);
   } catch (error) {
      document.write("<br>", error);
   } 
</script>
</body>
</html>

Output

The above program returns an itrator as −

String: TutorialsPointTuto
Regular expression: /Tuto/g
An iterator: Tuto,Tuto

Example 2

If the specified regular expression does not match in this string, it will return an empty iterator.

The following is another example of the JavaScript String matchAll() method. We use this method to retrieve an iterator of all results matches in this string "JavaScript" against the specified regex /java/[A-Z]*/g.

<html>
<head>
<title>JavaScript String matchAll() Method</title>
</head>
<body>
<script>
   const str = "JavaScript";
   document.write("String: ", str);
   const regex = /java[A-Z]*/g;
   document.write("<br>Regular expression: ", regex);
   //using the matchAll() function
   const iterator_array = [...str.matchAll(regex)];
   try {
      document.write("<br>An iterator: ", iterator_array);
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Output

After executing the above program, it will return an empty iterator as −

String: JavaScript
Regular expression: /java[A-Z]*/g
An iterator:

Example 3

If the regular expression (regexp) does not have the global (g) flag set or does not contain "g", it will throw a 'TypeError' exception.

In the following example, we use the JavaScript String matchAll() method to retrieve an iterator of all the results that match this string "Hyper Text Markup Language" against the specified regular expression /Lan[a-z]*/. Since the regular expression does not contain global("g"), It will throw a "TypeError" exception.

<html>
<head>
<title>JavaScript String matchAll() Method</title>
</head>
<body>
<script>
   const str = "Hyper Text Markup Language";
   document.write("String: ", str);
   const regex = /Lan[a-z]*/;
   document.write("<br>Regular expression: ", regex);
   //using the matchAll() function
   try {
      document.write("An iterators: ", str.matchAll(regex));
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Output

The above program throws the 'TypeError' exception.

String: Hyper Text Markup Language
Regular expression: /Lan[a-z]*/
TypeError: String.prototype.matchAll called with a non-global RegExp argument
Advertisements