SoFunction
Updated on 2025-04-06

How and tips to use Split in Javascript

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

<script language="JavaScript"> 
<!-- 
function Request(strName) 

var strHref = "/?a=1&b=1&c=Split instance";
var intPos = ("?"); 
var strRight = (intPos + 1); 
var arrTmp = ("&"); 
for(var i = 0; i < ; i++) 

var arrTemp = arrTmp[i].split("="); 
if(arrTemp[0].toUpperCase() == ()) return arrTemp[1]; 

return ""; 

alert(Request("a")); 
alert(Request("b")); 
alert(Request("c")); 
//--> 
</script> 

There is another way to obtain address bar parameters, regular expression:
Copy the codeThe code is as follows:

<script> 
 = function(name) 

var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); 
var r = (("\?")+1).match(reg); 
if (r!=null) return unescape(r[2]); return null; 

var str = "/?a=1&b=1&c=Split instance";
alert(("a")); 
alert(("b")); 
alert(("c")); 
</script>