Home  >  Article  >  Web Front-end  >  Split usage methods and techniques in Javascript_Basic knowledge

Split usage methods and techniques in Javascript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 19:17:131212browse

I rarely used the Split method before, but I found some information today.
Usage method: myArray=string.split("|")
Explanation: "|" is the cutting characteristic character, string is the string to be cut, myArray is the cutting result (stored in the array), use method myArray [n], n=myArray.length.
At first, I was thinking about how to get the maximum subscript of myArray using asp thinking. It was definitely not Ubound(myArray). I searched for a long time but couldn’t find it. Finally, I found out. In fact, in Javascript, arrays have a length attribute. myArray.length-1 is the maximum subscript of the myArray array. It is very simple. It is easy to think of it, but difficult to think of it.
Let’s look at an example of using split: using javascript to obtain address bar parameters.

Copy code The code is as follows:



There is another way to get the address bar parameters, regular expression Formula:
Copy code The code is as follows:

<script> <br>String.prototype .getQuery = function(name) <br>{ <br> var reg = new RegExp("(^|&)" name "=([^&]*)(&|$)"); <br> var r = this.substr(this.indexOf("?") 1).match(reg); <br> if (r!=null) return unescape(r[2]); return null; <br>} <br> var str = "www.nextway.cn/index.htm?a=1&b=1&c=Split instance"; <br>alert(str.getQuery("a")); <br>alert(str.getQuery("b ")); <br>alert(str.getQuery("c")); <br></script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn