468,777 Members | 2,225 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,777 developers. It's quick & easy.

split or regex difference between FF and IE

I don't know where the actual issue is,
but hopefully someone can explain.

The following displays "5" in FireFox, but "3" in IE:

<script type="text/javascript" language="javascript">
var newString = ",a,b,c,";
var treeArray = newString.split(/\,/i);
alert(treeArray.length);

</script>

Thanks,
Jim

Jul 23 '05 #1
1 2693
<jh*****@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I don't know where the actual issue is,
but hopefully someone can explain.

The following displays "5" in FireFox, but "3" in IE:

<script type="text/javascript" language="javascript">
var newString = ",a,b,c,";
var treeArray = newString.split(/\,/i);
alert(treeArray.length);

</script>

Thanks,
Jim


You've found one of the known issues with the String#split() method and
IE.

When you call split() using a String as the argument, IE includes empty
items in the resulting Array.

When you call split() using a RegExp as the argument, IE drops empty
items in the resulting Array.

var s = 'a#b##c###d';

// "4" - all Array elements contain non-empty strings
alert(s.split(/#/).length);

// "7" - Array elements 2, 4 and 5 are empty strings
alert(s.split("#").length);

Note that in Gecko-based browsers and Opera, both alert "7". So the
simplest solution is probably to use String#split() with a String as the
parameter instead of a RegExp.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by lgbjr | last post: by
7 posts views Thread by Jordi Rico | last post: by
1 post views Thread by mad.scientist.jr | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.