views:

38

answers:

1

Why the following pattern in IE and Firefox matches result different?

var str = 'a,b,c , d,   e   ,f';
var matches = str.split(/(\s+)?,(\s+)?/);
alert(matches);

IE: 
a,b,c,d,e,f

firefox: 
a,,,b,,,c, , ,d,,   ,e,   ,,f

how to match like IE result? please answer me :(

ie8 and firefox v3.6.8

+3  A: 
var str = 'a,b,c , d,   e   ,f';
var matches = str.split(/\s*,\s*/);
alert(matches);

The reason you are getting the extra entries in Firefox is because the parentheses (()) in your regular expression are captured as additional matches. This is normally the expected behaviour and I would argue that IE has a bug because it doesn’t do it. In my example, there are no parentheses in the regex, so you get only the text between the matches.

Timwi
Answer revoked, +1, since you actually knew why it was doing that xD
Matchu