Javascript String matchAll()

The matchAll() method returns an iterator of results after matching a string against a regular expression.

Example

// string definition
const sentence = "JavaScript1JavaScript2";

// a pattern having 'JavaScript' followed by a digit
const regex = /JavaScript\d/g;

// finding matches in the string for the given regular expression
let results = sentence.matchAll(regex);

// looping through the iterator
for (result of results) {
  console.log(result);
}

// Output:
// ["JavaScript1", index: 0, input: "JavaScript1JavaScript2", groups: undefined]
// ["JavaScript2", index: 11, input: "JavaScript1JavaScript2", groups: undefined]

matchAll() Syntax

The syntax of the matchAll() method is:

str.matchAll(regexp)

Here, str is a string.


matchAll() Parameters

The matchAll() method takes a single parameter:

  • regex - A regular expression object (Argument is implicitly converted to regex if it is a non-regex object)

Note:

  • If regex object does not have the /g flag, a TypeError will be thrown.
  • The g flag is for global search which means this flag indicates that we test the regular expression against all the matches in the string.

matchAll() Return Value

  • Returns an iterator containing the matches including the capturing groups.

Note: The returned iterator's each item will have the following additional properties:

  • groups- An object of named capturing groups having keys as the names and values as the captured matches.
  • index- The index of search where the result was found.
  • input - A copy of the search string.

Example 1: Using matchAll() Method

// string definition 
const sentence= "I am learning JavaScript not Java.";

// pattern having 'Java' with any number of characters from a to z
const regex =  /Java[a-z]*/gi;

// finding matches in the string for the given regular expression let result = sentence.matchAll(regex);
// converting result into an array console.log(Array.from(result));

Output

[
  'JavaScript',
  index: 14,
  input: 'I am learning JavaScript not Java.',
  groups: undefined
]
[
  'Java',
  index: 29,
  input: 'I am learning JavaScript not Java.',
  groups: undefined
]

In the above example, we have defined a regular expression regex with the /g flag. We have then invoked the matchAll() method in sentence.

sentence.matchAll(regex) matches the sentence string against the pattern that has- 'Java' along with any number of characters from a to z.

The method found two matches- 'JavaScript' and 'Java' for the given regex.

Note: The resulting output from the matchAll() method is in object so we have used Array.from(result) to convert it to array.


Example 2: Case Sensitive regex in matchAll()

The regular expression(regex) is case sensitive. We can use the i flag to make it case insensitive in the matchAll() method. For example:

// string definition 
const bio = "His name is  Albert and albert likes to code.";

// pattern having 'albert' or 'Albert'  
const regex = /albert/gi;

// finding 'albert' or 'Albert' in the string const result = bio.matchAll(regex);
console.log(Array.from(result));

Output

[
  [
    'Albert',
    index: 13,
    input: 'His name is  Albert and albert likes to code.',
    groups: undefined
  ],
  [
    'albert',
    index: 24,
    input: 'His name is  Albert and albert likes to code.',
    groups: undefined
  ]
]

Here, we have used i in regex along with g (/albert/g) which makes it case insensitive. So the method returns an array with two iterators with found matches- 'Albert' and 'albert'.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges