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.
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- Laravel Option Select - Default Issue
- How to toggle on Order in ReactJS
- PHP Recursively File Folder Scan Sorted by Modific
You can use
match
with the global modifier: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:
The output will be:
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.