url解析地址

 1 function urlParse (search) {
 2     let index = search.lastIndexOf('?')
 3     let url = search.substr(index) || window.location.search
 4     let obj = {}
 5     let reg = /[?&][^?&]+=[^?&]+/g
 6     let arr = url.match(reg)
 7     if (arr) {
 8         arr.forEach((item) => {
 9             let tempArr = item.substring(1).split('=')
10             let key = decodeURIComponent(tempArr[0])
11             let val = decodeURIComponent(tempArr[1])
12             obj[key] = val
13         })
14     }
15     return obj
16 }
17 // urlParse('https://www.baidu.com/index?a=1&b=3')
18 // {a: "1", b: "3"}
19 // urlParse()
20 // f:"8",ie:"utf-8",oq:"substr",rqlang:"cn",……}

 

posted on 2018-04-23 23:33  _linka  阅读(147)  评论(0编辑  收藏  举报