471,354 Members | 1,042 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

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 2766
<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
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.