Javascript uses regular expressions to obtain a parameter in the url, including javascripturl.

Source: Internet
Author: User

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

Related Article

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.