On Jul 11, 10:21*pm, Peter Michaux <petermich...@gmail.com> wrote:
> On Jul 11, 10:17*pm, GenghisOne <mdkach...@gmail.com> wrote:
>
>
>
> > I am totally confused by this string.split thing..
>
> > When I try this...
> > var myString = "one, two, three";
> > myArray = myString.split(",");
> > document.write(myArray.length);
>
> > ...I get back 3 items. This makes sense to me.
>
> > But when I try this...
> > var myString = "<p>one</p><p>two</p><p>three</p>";
> > myArray = myString.split("</p>");
> > document.write(myArray.length);
>
> > ...I get back 4 items. What gives? Shouldn't myArray.length return 3
> > as well.
>
> > Any help would be most sincerely appreciated and my apologies in
> > advance for such a basic question.
>
> Your HTML string ends in </p>. In string.split land, there is an empty
> string after that third occurrence of </p> and so there are four
> elements in the result.
>
> Firebug console session...
>
> >>> "<p>one</p><p>two</p><p>three</p>".split("</p>");
>
> ["<p>one", "<p>two", "<p>three", ""]
>
> Peter
Hi Peter
Thanks for the insights...
After looking at your Firebug console results, I started to ask
myself...where did my </p> tags go? And then it dawned on me...the
split function deletes the split term you use.
So maybe what I'm really trying to ask is this...
What's the best way to convert the following string into an array?
var myString = "<p>one</p><p>two</p><p>three</p>";
In my mind, the perfect conversion function would bring back an array
of three items.
Thx much.
|