Methods and techniques of split usage in JavaScript-basics

Source: Internet
Author: User
Rarely used the split method before, today to find some information.
How to use: Myarray=string.split ("|")
Description: "|" To cut the character character, string is the string to be cut, myarray is the result of the cut (stored in an array), and the method myarray[n],n=myarray.length is used.
At first I also use the thinking in the ASP to consider how to get the largest subscript myarray, certainly not UBound (myarray), looking for a half-day did not find, finally know, in fact, in the JavaScript array has a length property, Myarray.length-1 is also the myarray array of the largest subscript, very simple, it is easy to think, it is difficult to imagine.
Take a look at a split example: use JavaScript to get the address bar parameter.
Copy Code code as follows:

<script language= "JavaScript" >
<!--
function Request (strName)
{
var strhref = "Www.nextway.cn/index.htm?a=1&b=1&c=Split instance";
var intpos = Strhref.indexof ("?");
var strright = strhref.substr (intpos + 1);
var arrtmp = Strright.split ("&");
for (var i = 0; i < arrtmp.length; i++)
{
var arrtemp = arrtmp[i].split ("=");
if (arrtemp[0].touppercase () = = Strname.touppercase ()) return arrtemp[1];
}
Return "";
}
Alert (Request ("a"));
Alert (Request ("B"));
Alert (Request ("C"));
-->
</script>

There is another way to get the Address bar argument, regular expression:
Copy Code code as follows:

<script>
String.prototype.getQuery = function (name)
{
var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)");
var r = this.substr (This.indexof ("\?") +1). Match (REG);
if (r!=null) return unescape (r[2]); return null;
}
var str = "Www.nextway.cn/index.htm?a=1&b=1&c=Split instance";
Alert (Str.getquery ("a"));
Alert (Str.getquery ("B"));
Alert (Str.getquery ("C"));
</script>

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.