split or regex difference between FF and IE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jhcorey@yahoo.com

    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="javas cript">
    var newString = ",a,b,c,";
    var treeArray = newString.split (/\,/i);
    alert(treeArray .length);

    </script>

    Thanks,
    Jim

  • Grant Wagner

    #2
    Re: split or regex difference between FF and IE

    <jhcorey@yahoo. com> wrote in message
    news:1105639132 .095572.18020@f 14g2000cwb.goog legroups.com...[color=blue]
    >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="javas cript">
    > var newString = ",a,b,c,";
    > var treeArray = newString.split (/\,/i);
    > alert(treeArray .length);
    >
    > </script>
    >
    > Thanks,
    > Jim[/color]

    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 <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq


    Comment

    Working...