JS regular parse URL parameter sample code _ regular Expression

Source: Internet
Author: User
Copy Code code as follows:

var url = "http://127.0.0.1/e/action/ShowInfo.php?classid=9&id=2";//define Variable
function Parse_url (_url) {//Definition functions
var pattern =/(\w+) = (\w+)/ig;//define regular expression
var parames = {};//defined array
Url.replace (pattern, function (A, B, c) {parames[b] = c;});
/* This is the most critical. When replace is matched to classid=9. Then use the execution function (a,b,c), where a value is: Classid=9,b value is classid,c to 9; because in the definition The regular expression has a two-child match. The value of the array key is then assigned to 9, and then the ClassID is completed. Then continue to match to id=2, at which point the function (a,b,c) is executed, and the value of a is: Id=2,b is id,c with a value of 2, and the value of the array's key as the ID is assigned to 2.*/
Return parames;//returns this array.
}
var parames = parse_url (URL);
Alert (parames[' classid '] + "," + parames[' id '); print the corresponding value of the array according to the key value

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.