解析url查询字符串高级方法

 

    function queryURLParams(url) {
      let result = {},
        reg1 = /([^?=&#]+)=([^?=&#]+)/g,
        reg2 = /#([^?=&#]+)/g;
      url.replace(reg1, (n, x, y) => result[x] = y)
      url.replace(reg2, (n, x) => result['HASH'] = x)
      return result
    }

    let url = 'http://www.baidu.com/index?name=baidu&age=35#333'
    let paramsObj = queryURLParams(url)
    console.log(paramsObj)

你可能感兴趣的:(JavaScript)