JS extracts parameter names and values from URL addresses

Source: Internet
Author: User
Tags regular expression


Getting the parameter value, given the parameter name, is easy to do with regular expressions.


JS implementation method is as follows:

The code is as follows Copy Code

function GetValue (URL, name) {
var reg = new RegExp (' (\?| &) ' + name + ' = ([^&?] *) ', ' I ';
var arr = Url.match (reg);

if (arr) {
return arr[2];
}

return null;
}

Or

Method One: Regular Analysis method

The code is as follows Copy Code

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;
}

This call:

Alert (getquerystring ("parameter name 1"));
Alert (getquerystring ("parameter Name 2"));
Alert (getquerystring ("parameter name 3"));

If you want to get all the parameter names and their corresponding values, you can also use the method of the regular expression.


JS Implementation method:

The code is as follows Copy Code

function Getkeyvalue (URL) {
var result = {};
var reg = new RegExp (' ([\?| and]) (. +?) = ([^&?] *) ', ' IG ';
var arr = reg.exec (URL);

while (arr) {
RESULT[ARR[2]] = arr[3];

arr = reg.exec (URL);
}

return result;
}

Note that there is also a method for matching in JS called Match,match is a string, and exec is the method of RegExp object. Using the match method of a string, while the regular expression is specified as a global match, the grouping in the regular expression is not used and the result returned is a substring of all matching regular expressions. When the Exec method does not use a global match flag, the first matched substring of the time returned, if the global match flag is used, the first time a string that matches the symbol from scratch is invoked again, starting with the last match.

Here's how to implement PHP:

The code is as follows Copy Code

function Getkeyvalue ($url) {
$result = Array ();
$MR = Preg_match_all ('/? | &) (. +?) = ([^&?] *)/I ', $url, $matchs);

if ($mr!== FALSE) {
for ($i = 0; $i < $MR; $i + +) {
$result [$matchs [2][$i]] = $matchs [3][$i];
}
}

return $result;
}

The Preg_match_all method of PHP holds the matching result in the third specified parameter, which is a two-dimensional array. The first dimension is an array of grouped information, where the first array holds all the matching complete strings, and the second array holds the first () worthy, and the second dimension is the value of the grouping.

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.