Alex Rivera | Logout

preg_match_all JS equivalent?

Asked 2009-06-11T21:25:39.717
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.

Edit
Report

1 Answer

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.

answered 2009-06-11T21:30:48.580

Your Answer