Home  >  Article  >  Web Front-end  >  Example tutorial on getting the parameter name in url using js

Example tutorial on getting the parameter name in url using js

零下一度
零下一度Original
2017-06-24 13:49:301176browse
It is rare to use js to obtain the parameter name and parameter value in the URL, but I need to do this today. Let me introduce to you the specific example method.


When the parameter name is known, getting the parameter value can be easily done using regular expressions.


js implementation method is as follows:

The code is as follows

function getValue(url, name) {
var reg = new RegExp('(\?|&)' + name + '=([^&?]*)', 'i') ;
var arr = url.match(reg);

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

return null;
}

or

Method 1: Regular analysis method

## The code is as follows##function getQueryString(name) { If you want To get all parameter names and their corresponding values, you can also use regular expressions.

var reg = new RegExp("(^|&) " + name + "=([^&]*)(&|$)", "i");

var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}

Call like this:

alert(GetQueryString("Parameter name 1"));

alert(GetQueryString("Parameter name 2"));

alert(GetQueryString("Parameter name 3"));

js implementation method:


The code is as follows function getKeyValue(url) {Note that there is another method called match used for matching in js. Match is a method of string, and exec is a method of RegExp object. If you use the match method of a string and specify the regular expression as a global match, then the grouping in the regular expression will be useless, and the returned result will be all substrings that match the regular expression. When the exec method does not use the global match flag, the time returned is the first matched subcharacter. If the global match flag is used, the string that matches the symbol is executed from the beginning for the first time. If called again, it will start from the last matching result. match.

var result = {};

var reg = new RegExp('( [\?|&])(.+?)=([^&?]*)', 'ig');
var arr = reg.exec(url);

while (arr ) {

result[arr[2]] = arr[3];


arr = reg.exec(url);

}


return result;

}


The implementation method of PHP is provided below:

The code is as followsfunction getKeyValue($url) {#php's preg_match_all method stores the matching results in the third specified parameter, which is a two-dimensional array. The first dimension is an array of grouping information, that is, the first array stores all matching complete strings, the second array stores the value corresponding to the first (), and the second dimension is the grouping value.

$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 above is the detailed content of Example tutorial on getting the parameter name in url using js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn