简体   繁体   English

IE和Firefox正则表达式问题

[英]IE and Firefox regular expression question

Why the following pattern in IE and Firefox matches result different? 为什么IE和Firefox中的以下模式匹配结果不同?

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? 如何匹配像IE一样的结果? please answer me :( 请回答我 :(

ie8 and firefox v3.6.8 ie8和firefox v3.6.8

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. 之所以在Firefox中获得额外的条目,是因为正则表达式中的括号( () )被捕获为其他匹配项。 This is normally the expected behaviour and I would argue that IE has a bug because it doesn't do it. 这通常是预期的行为,我认为IE有一个错误,因为它没有执行。 In my example, there are no parentheses in the regex, so you get only the text between the matches. 在我的示例中,正则表达式中没有括号,因此您只获得匹配项之间的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM