-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// void 0 return undefined | ||
|
||
// 1 | ||
// str: 模式的匹配项;key: 第一个捕获组的匹配项;value: 第二个捕获组的匹配项。 | ||
function getUrlParam(sUrl, sKey){ | ||
var result = {}; | ||
sUrl.replace(/\??(\w+)\s?=\s?(\w+)&?/g, function(str, key, value){ | ||
if(result[key] !== undefined | void 0){ | ||
result[key] = [].concat(result[key], value); | ||
}else{ | ||
result[key] = value; | ||
} | ||
}); | ||
if(sKey === void 0){ | ||
return result; | ||
}else{ | ||
return result[sKey] || ''; | ||
} | ||
} | ||
// 2 | ||
function getUrlParam(sUrl, sKey) { | ||
var url = sUrl.slice(sUrl.indexOf('?') + 1, sUrl.indexOf('#')).split('&') | ||
var ret, obj = {}; | ||
for (var i in url) { | ||
var arr = url[i].split('=') | ||
obj[arr[0]] === void 0 ? obj[arr[0]] = arr[1] : obj[arr[0]] = [].concat(obj[arr[0]], arr[1]) | ||
} | ||
sKey === void 0 ? ret = obj : ret = obj[sKey] | ||
console.log(ret) | ||
return ret | ||
} | ||
getUrlParam('http://www.nowcoder.com?key=1&key=2&key=3&test=4#hehe', 'key') | ||
|
||
// 3 | ||
function getUrlParam(sUrl, sKey) { | ||
var url = sUrl.slice(sUrl.indexOf('?') + 1, sUrl.indexOf('#')).split('&') | ||
var ret, obj = {}; | ||
for (var i in url) { | ||
var arr = url[i].split('=') | ||
if (obj[arr[0]] !== void 0) { | ||
obj[arr[0]] = [].concat(obj[arr[0]], arr[1]) | ||
} else { | ||
obj[arr[0]] = arr[1] | ||
} | ||
} | ||
if (sKey === void 0) { | ||
ret = obj | ||
} else { | ||
ret = obj[sKey] || '' | ||
} | ||
console.log(ret) | ||
return ret | ||
} |