How to get the parameters from the URL is a very basic problem. First assume that the URL is legal, code as follows, welcome you big code Review.
1. You can use a match and loop split
function findqueriesfromurl (URL) { var regex, matches, I, length, pair, result = {}, query; if return ; =/\w+\=\w*/g; = Url.match (regex); for (i = 0, length = matches.length;i<length;i++) { = matches[i].split (' = '); Result[decodeuricomponent (pair[0])] = decodeURIComponent (pair[1]); } return result;}
2. You can use EXEC to get all the groupings,
function findqueriesfromurl (URL) { var regex, result = {}, match; if return ; =/(\w+) \= (\w*)/g;//No/g will die loop. while (match = regex.exec (URL)) {
result[decodeuricomponent (match[1])] = decodeURIComponent (match[2]); } return result;}
It is important to note that exec only iterates over the same calling object and the reference of the same string as the argument , otherwise it may die loops, such as the following code:
while (match = (/(\w+) \= (\w*)/g). EXEC (URL)) {result[match[1]] = match[2];}
aside, the EXEC function loops back all of the match's objects, such as calling
(function excute (URL) { var regex, result = {}, match, i = 0; =/(\w+) \= (\w*)/g; while (I <) { i+ +; Console.log (regex.exec (URL)); } return result;}) (' a=b ');
Will loop through the output of a match object and NULL, as follows:
["A=b", "a", "B", index:0, Input: "A=b"] NULL ["A=b", "a", "B", index:0, Input: "A=b"] NULL ["A=b", "a", "B", index:0, Input: "A=b"] NULL ["A=b", "a", "B", index:0, Input: "A=b"] NULL ["A=b", "a", "B", index:0, Input: "A=b"] NULL
If you remove the g inside the regular expression, the output will become:
["A=b", "a", "B", index:0, Input: "A=b"] ["A=b", "a", "B", index:0, Input: "A=b"] ["A=b", "a", "B", Inde x:0, Input: "A=b"["A=b", "a", "B", index:0, Input: "A=b"] ["A=b", "a", "B", index:0, Input: "A=b" ["A=b", "a", "B", index:0, Input: "A=b"] ["A=b", "a", "B", index:0, Input: "A=b"] ["A=b", "a", "B" , index:0, Input: "A=b"["A=b", "a", "B", index:0, Input: "A=b"] [
This will not be able to take all the matches.
JavaScript gets parameters from URL