preg_match_all JS equivalent?

Alex S picture Alex S · Jun 11, 2009 · Viewed 28k times · Source

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.

Answer

Paolo Bergantino picture Paolo Bergantino · Jun 11, 2009

You can use match with the global modifier:

>>> '1 2 3 4'.match(/\d/g);
["1", "2", "3", "4"]