I\'m new to JavaScript and have a question about regular expressions. I have the following code:
var patt3=new RegExp(/(July|August)\\s+\\d{1,2}(\\s|,)\\d{4}
g
only matches all occurrences of the entire pattern. It is not analogous to matching groups like PHP's preg_match_all
. Without g
, it only matches the first occurrence of the pattern (matches[0]
) and all matched groups are the other array elements.
If you wanted to get nested matches as well, you can use the concepts described by John Resig to do so:
str3.replace(patt3, function () {
//This is the first captured group in all matches iterated over
console.log(arguments[1]);
});