Javascript uses regular expressions to obtain a parameter in the url, including javascripturl.
function GetUrlMenuCode() { var url = window.location.href; var parameter = url.substring(url.indexOf('?') + 1); parameter = parameter.split('&'); var reg = /MenuCode=/g; var menuCode = ""; for (var i = 0; i < parameter.length; i++) { reg.lastIndex = 0; if (reg.test(parameter[i])) { menuCode = parameter[i].replace("MenuCode=", ""); break; } } return menuCode; }
The preceding example shows how to obtain a parameter named "MenuCode" from the url.
To obtain the URL parameter value through JS regular expression, you need to explain it to someone else.
Var url = "127.0.0.1/... 9 & id = 2"; // define the variable
Function parse_url (_ url) {// define a function
Var pattern =/(\ w +) = (\ w +)/ig; // define a regular expression
Var parames = {}; // defines an array
Url. replace (pattern, function (a, B, c ){
Parames [B] = c;
});
/* This is the key. when replace matches classid = 9. execute function (a, B, c). The value of a is: classid = 9, the value of B is classid, and the value of c is 9. (This is a reverse reference. because there are two child matches when defining a regular expression .)
Then, assign the key of the array as classid to 9, and then complete.
Then match to id = 2. Then, execute function (a, B, c). The value of a is id = 2, and the value of B is id, the value of c is 2, and then the key of the array is assigned as id 2.
*/
Return parames; // return this array.
}
Var parames = parse_url (url );
Alert (parames ['classid '] + "," + parames ['id']); // print the value of the array based on the key value.
A simple regular expression matches parameters in a URL
// Obtain the parameters in the url (name is used)
Function getQueryString (name ){
Var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) (& | $)", "I ");
Var r = window. location. search. substr (1). match (reg );
If (r! = Null) return unescape (r [2]);
Return null;
}
Name is the value you pass, as you said I, caid, u
Extra points