Differences between the string split methods of JS in different browsers

Source: Internet
Author: User

Well, recently in a variety of direct code debugging, found that the problem may be a little more, let's write down, this essay may be updated from time to time, because I believe different browser differences between a lot.

Directly on the code

1 <HTML>2     <Head>3     </Head>4 5     <Body>6         <Script>7             varStr= "/part1/part2";8             var_list=Str.split (/[\\/]/);9              for(varI= 0; I<_list.length; I++)Ten             { One                 varT= "_list[" +I+ "]  :  " + "\ '" +_list[i]+ "\ '"; A alert (t); -             } -         </Script> the     </Body> - </HTML>

Open FF, Chrome, and IE8 separately to see the results, and find that the result is different

where FF and chrome results are the same

[0] = = "

[1] = ' part1 '

[2] = ' part2 '

And the IE8 is

[0] = ' part1 '

[1] = ' part2 '

So if you use _list by index, there will be different results between natural browsers.

For a different set of code, note that the var str = line of code differs

1 <HTML>2     <Head>3     </Head>4 5     <Body>6         <Script>7             varStr= "part1/part2/";8             var_list=Str.split (/[\\/]/);9              for(varI= 0; I<_list.length; I++)Ten             { One                 varT= "_list[" +I+ "]  :  " + "\ '" +_list[i]+ "\ '"; A alert (t); -             } -         </Script> the     </Body> - </HTML>

FF is still consistent with Chrome's performance,

[0] = ' part1 '

[1] = ' part2 '

[2] = = "

and IE8 is

[0] = ' part1 '

[1] = ' part2 '

The following example is no longer lifted, but one rule that can be found is that IE8 the empty string after the split is swallowed ... This is simply a no-no-words ...

However, when you do not use regular expressions, IE8 is not a problem, pay attention to var _list = Str.split ("/"); Instead of the regular var _list = Str.split (/[\\/]/);

<HTML>    <Head>    </Head>    <Body>        <Script>            varStr= "part1//part2/"; var_list=Str.split ("/");  for(varI= 0; I<_list.length; I++)            {                varT= "_list[" +I+ "]  :  " + "\ '" +_list[i]+ "\ '";            Alert (t); }        </Script>    </Body></HTML>

Well, IE8 again full of blood ...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.