精简版url参数解析,附带hash参数
来源:18-11 面试讲解-10:解析 url 参数

1007123589qaz
2020-11-09
String.prototype.myQueryUrlParameter=function(){
let obj={}
this.replace(/([^?&=#]+)=([^?&=#]+)/g,function(){
obj[arguments[1]]=arguments[2]
})
this.replace(/#([^?&=#]+)$/g,function(){
obj['HASH']=arguments[2]
})
return obj
}
let url=`http://www.baidu.com?name=tom&age=20#teacher`;
let result=url.myQueryUrlParameter();
console.log(result)
写回答
1回答
-
解析的没问题。
不建议把它放在 String 原型上,毕竟不是所有的字符串都需要这样的方法。
012020-11-10
相似问题