Home >Web Front-end >JS Tutorial >How to Extract All Matches from a String Using RegExp.exec()?
RegExp to Extract Multiple Matches Using RegExp.exec
To extract all matches from a string using RegExp.exec, you can continue calling re.exec(s) in a loop. The following code snippet demonstrates this:
var re = /\s*([^[:]+):\"([^"]+)"/g; var s = '[description:"aoeu" uuid:"123sth"]'; var m; do { m = re.exec(s); if (m) { console.log(m[1], m[2]); } } while (m);
With the provided test string '[description:"aoeu" uuid:"123sth"]', this code will output:
description aoeu uuid 123sth
Note that the regular expression used here:
To test this solution, you can use the provided JSFiddle link: https://jsfiddle.net/7yS2V/.
The above is the detailed content of How to Extract All Matches from a String Using RegExp.exec()?. For more information, please follow other related articles on the PHP Chinese website!