Skip to content

Instantly share code, notes, and snippets.

@hkongm
Created September 6, 2013 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkongm/6460929 to your computer and use it in GitHub Desktop.
Save hkongm/6460929 to your computer and use it in GitHub Desktop.
function ParseURL(arg){
var url = arg,
regexp = /([^\?]*)\??([^#]*)#(.*)/g,
regexpParam = /([^&]*)=([^&]*)/g,
urlExec = regexp.exec(url),
_host = urlExec[1],
_urlparam = urlExec[2],
_hashparam = urlExec[3];
function parseParam(paramStr){
var param = {},
reg = paramStr.match(regexpParam);
for(var i=0,rl=reg.length;i
var item = reg[i],
itemSplit = item.split('=');
itemKey = itemSplit[0],
itemVal = itemSplit[1];
param[itemKey] = itemVal;
}
return param;
}
// 获取url的参数
function urlParam(){
return parseParam(_urlparam);
}
//获取hash的参数
function hashParam(){
return parseParam(_hashparam);
}
this.param = urlParam;
this.hashParam = hashParam;
//按关键词搜索并返回value
function search(key){
var regexp = new RegExp(key+'=[^&]*','g'),
urlMatch = url.match(regexp);
for(var i=0,ul=urlMatch.length;i
urlMatch[i]=urlMatch[i].split('=')['1'];
}
return urlMatch;
}
this.search=search;
this.scheme = /^([^:]*):/.exec(_host)[1];
this.host = /\/\/([^\/]*)\//.exec(_host)[1];
try{
this.port = /:(\d*)$/.exec(_host)[1];
}catch(e){
this.port = '8080';
}
this.path = new RegExp(this.host+'([^:]*)').exec(_host)[1];
}
var parseURL = new ParseURL('http://image.baidu.com/fdsa/fd324/23442/i?ct=201326592&cl=2&nc=1&lm=-1&st=-1&tn=baiduimage&istype=2&fm=&pv=&z=0&word=��վǰ�˿���&fr=wenku&&&&&mmm=11#&st=-1&tn=baiduimage&istype=2&fm=&pv=&z=0&word=��վǰ�˿���&fr=wenku&tn=tntest&&&&qq=605003402');
console.log(parseURL.param());
console.log(parseURL.hashParam());
console.log(parseURL.search('qq'));
console.log(parseURL.scheme);
console.log(parseURL.host);
console.log(parseURL.port);
console.log(parseURL.path);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment