19

Is there an equivalent of PHP's preg_match_all in Javascript? If not, what would be the best way to get all matches of a regular expression into an array? I'm willing to use any JS library to make it easier.

Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
Alex S
  • 25,241
  • 18
  • 52
  • 63

5 Answers5

33

You can use match with the global modifier:

>>> '1 2 3 4'.match(/\d/g);
["1", "2", "3", "4"]
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • 2
    I didn't understand that syntax. – m3nda Feb 24 '15 at 23:18
  • @erm3nda i think **/\d/g** is you didn't understand. Here [Creating a regular expression in Javascript](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions). – Mohammed H Feb 21 '17 at 11:42
  • Note that this approach ignores captures. `'1a 2b 3c 4d'.match(/(\d)([a-z])/g)` outputs just `["1a", "2b", "3c", "4d"]`. If you need captures see [this answer](https://stackoverflow.com/a/1222072/2750743) – Klesun Dec 03 '18 at 16:47
10

John Resig has written about a great technique on his blog called 'Search and dont replace'

It works using javascript's replace function, which takes a callback function, and returns nothing to leave the original content unaltered.

This can be a neater than using a global match and iterating over an array of results, especially if you're capturing several groups.

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
3

A better equivalent of preg_match_all from PHP in JS would be to use the exec() function. This will allow you to capture groups as well, with match() you can not do that.

For example you want to capture all times and the number in brackets from the variable myString:

var myString = "10:30 am (15 left)11:00 am (15 left)11:30 am";
var pattern = /(\d{1,2}:\d{1,2}\s?[ap]m)\s\((\d+)/gi;
var match;
while (match = pattern.exec(myString)){
  console.log('Match: "' + match[0] + '" first group: -> "' + match[1] + '" second group -> ' + match[2]);
}

The output will be:

Match: "10:30 am (15" first group: -> "10:30 am" second group -> 15
Match: "11:00 am (15" first group: -> "11:00 am" second group -> 15
Constantin Stan
  • 1,125
  • 9
  • 13
1

If you need the exactly return structure as in php. Please check for yourself before usage)

Solution (on typescript):

function preg_match_all(regex: RegExp, str: string) {
  return [...str.matchAll(new RegExp(regex, 'g'))].reduce((acc, group) => {
    group.filter((element) => typeof element === 'string').forEach((element, i) => {
      if (!acc[i]) acc[i] = [];
      acc[i].push(element);
    });

    return acc;
  }, [] as string[][]);
}

Example (on javascript):

const str = ';9N;N1;CP-S2;EU;CP-S3;E;CP-S4;KRT;VH;;VL;CP-S2;CP-S3;CP-S4';
const regex = /CP-(S[\d*])/;
const result = preg_match_all(regex, str);

console.log(result);

function preg_match_all(regex, str) {
  return [...str.matchAll(new RegExp(regex, 'g'))].reduce((acc, group) => {
    group.filter((element) => typeof element === 'string').forEach((element, i) => {
      if (!acc[i]) acc[i] = [];
      acc[i].push(element);
    });

    return acc;
  }, []);
}
0

Use new RegExp().test()

function preg_match_all(regex, str) {
  return new RegExp(regex,'g').test(str)
}
Justin Liu
  • 581
  • 6
  • 21