offline version v3


85 of 264 menu

matchAll method

The matchAll method returns all matches with regular expression as an iterable object, each element of which contains an array of found and its capturing groups. The method can only be called with the g modifier. If there are no matches, it will return null.

Syntax

string.matchAll(regular expression);

Example

Let's get all matches and loop through them:

let str = '12 34 56'; let matches = str.matchAll(/(\d)(\d)/g); for (let match of matches) { console.log(match); }

The code execution result:

[12, 1, 2] [34, 3, 4] [56, 5, 6]

Example

Let's convert the iterable object to a regular array:

let str = '12 34 56'; let matches = str.matchAll(/(\d)(\d)/g); let res = Array.from(matches); console.log(res);

The code execution result:

[ [12, 1, 2], [34, 3, 4], [56, 5, 6] ]

See also

  • the test method
    that checks a string
  • the match method
    that searches for matches in a string
  • the exec method
    that performs a sequential search
  • the replace method
    that performs search and replacement
  • the search method
    that performs a search
  • the split method
    that splits a string
enru