While doing some localization optimization for JavaScript templates that load the localized strings with AJAX calls, I ran into some strange behaviour in the JavaScript RegExp object. I had to parse through a string that contains calls to dicole.msg function. The most straightforward way to do this was of course with a regexp match. This is where the strangeness occurred. There seems to be no obvious way to get all matches and their subgroups with JavaScript regular expressions. This is a test I used to confirm this:
http://rikshot.ath.cx/jsregexptest.html
Now if you analyze that for a second, it doesn’t really make sense.
- string.match(pattern) returns the matched string and the subgroups as expected
- string.match(global_pattern) returns all the matched strings but no subgroups, this is also okay (is it, really?)
- pattern.exec(string) returns the matched string and the subgroups as expected
- global_pattern.exec(string) returns the first matched string and the subgroups, this is where the problem lies
For me at least, it would make more sense if global_pattern.exec(string) would return all the matched strings and their subgroups. Now, the only way to get this result is to first use the string.match(global_pattern) to get all the matches and then run through that array using either the string.match(pattern) or the pattern.exec(string) method of capturing the subgroups. Why doesn’t the global_pattern.exec(string) return an array of arrays that contain all matched strings and their subgroups, as would be expected?
Edit: Tested this on the newest Firefox, Opera, Chrome and Safari. All displayed the same results. Now perhaps the Microsoft boys have noticed this issue, because from IE6 to IE8, the global_pattern.exec(string) returns null. Funny.