Regular expressions, JavaScript and cross browser hell
Posted On Wednesday, 5 March 2008 at at 17:27 by Rick WalshUsing regular expressions with JavaScript can be frustrating.
Specifically, trying to use regular expressions consistently with the different web browsers (Explorer and Firefox) is frustrating.
For example, using the match function upon a string yields different objects.
Firefox returns an Array containing the matches and Explorer returns an Array with the additional properties :
- The original string
- The matches (attached to properties 0 to n-1 ( where n = number of matches)
- A bunch of indexes.
var myMatches = mystring.match(myRegEx);
for(var match in matches ) {
alert(matches[match]);
}So, the code above will alert out just the matches in Firefox.Explorer will alert out the matches and the values of the additional properties too.
Which is correct? Time to read the specs (http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf).
Section 15.10.6.2 is pretty explicit. Explorer implements the functionality described in the specs, but the additional properties we mentioned above, are bespoke.
Damn you Microsoft.
