How to parse the Split in Javascript

Source: Internet
Author: User

Do you know how to use Split in Javascript? I will give you a brief introduction here. I believe this article will surely help you gain some benefits.

How to use Split in Javascript

Usage: myArray = string. split ("| ")

Note: "|" indicates the feature character to be cut, string indicates the string to be cut, and myArray stores the cut result in an array. Use myArray [n], n = myArray. length.

At first, I used the thinking in asp to think about how to get the maximum subscript of myArray. It must not be Ubound (myArray). I did not find it for a long time, and finally I got it, in fact, arrays in Javascript have a length attribute, myArray. length-1 is the maximum subscript of the myArray array. It's easy to think about it.

Let's look at an example using split: use javascript to get the address bar parameters.

 
 
  1. <Scriptlanguagescriptlanguage = "JavaScript">
  2. <! --
  3. FunctionRequest (strName)
  4. {
  5. VarstrHref = "www.nextway.cn/index.htm? A = 1 & B = 1 & c = Split instance ";
  6. VarintPos = strHref. indexOf ("? ");
  7. VarstrRight = strHref. substr (intPos + 1 );
  8. VararrTmp = strRight. split ("&");
  9. For (vari = 0; I <arrTmp. length; I ++)
  10. {
  11. VararrTemp = arrTmp [I]. split ("= ");
  12. If (arrTemp [0]. toUpperCase () = strName. toUpperCase ())
  13. ReturnarrTemp [1];
  14. }
  15. Return "";
  16. }
  17. Alert (Request (""));
  18. Alert (Request ("B "));
  19. Alert (Request ("c "));
  20. // -->
  21. </Script>
  22.  

There is another way to obtain the address bar parameters, the regular expression:

 
 
  1. <Script>
  2. String. prototype. getQuery = function (name)
  3. {
  4. Varreg = newRegExp ("(^ | &)" + name + "= ([^ &] *) (& | $ )");
  5. Varr = this. substr (this. indexOf ("? ") + 1). match (reg );
  6. If (r! = Null) returnunescape (r [2]); returnnull;
  7. }
  8. Varstr = "www.nextway.cn/index.htm? A = 1 & B = 1 & c = Split instance ";
  9. Alert (str. getQuery (""));
  10. Alert (str. getQuery ("B "));
  11. Alert (str. getQuery ("c "));
  12. </Script>
  13.  

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.