Last active
          March 18, 2021 11:31 
        
      - 
      
- 
        Save sofish/505e3c63f08dee01e543 to your computer and use it in GitHub Desktop. 
    URL Parser
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | var parser = function(url) { | |
| var a = document.createElement('a'); | |
| a.href = url; | |
| var search = function(search) { | |
| if(!search) return {}; | |
| var ret = {}; | |
| search = search.slice(1).split('&'); | |
| for(var i = 0, arr; i < search.length; i++) { | |
| arr = search[i].split('='); | |
| var key = arr[0], value = arr[1]; | |
| if(/\[\]$/.test(key)) { | |
| ret[key] = ret[key] || []; | |
| ret[key].push(value); | |
| } else { | |
| ret[key] = value; | |
| } | |
| } | |
| return ret; | |
| }; | |
| return { | |
| protocol: a.protocol, | |
| host: a.host, | |
| hostname: a.hostname, | |
| pathname: a.pathname, | |
| search: search(a.search), | |
| hash: a.hash | |
| } | |
| }; | |
| var url = 'http://sub.example.com:8023/home/?foo=bar&ciao=cc&arr[]=1&arr[]=2#hash'; | |
| parser(url); | |
| // 结果是? | 
山寨走了
(function(exportName) {
// var url = 'http://sub.example.com:8023/home/?foo=bar&ciao=cc#hash';
// console.log(JSON.stringify(jurls.parser(url), null, 2));
  var exports = exports || {};
  var urlCaches = {};
  var urlElement;
  var urlAttrs = ['protocol', 'host', 'hostname', 'pathname', 'search', 'hash', 'port'];
  var parser = function(url) {
    if (urlCaches[url]) {
      return urlCaches[url];
    }
    if (!urlElement) {
      urlElement = document.createElement('a');
    }
    urlElement.href = url;
    var result = {};
    for (var i = 0; i < urlAttrs.length; i++) {
      result[urlAttrs[i]] = urlElement[urlAttrs[i]];
    }
    urlCaches[url] = result;
    return result;
  };
  exports.parser = parser;
  if (typeof define === 'function') {
    if (define.amd || define.cmd) {
      define(function() {
        return exports;
      });
    }
  } else if (typeof module !== 'undefined' && module.exports) {
    module.exports = exports;
  } else {
    window[exportName] = exports;
  }
})('jurls');a.pathname在chrome下是带开头的/的,IE下不带,a.port默认情况下IE是输出80的,而chrome是输出空字符串。。
      ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
       12            3  4          5       6  7        8 9
   The numbers in the second line above are only to assist readability;
   they indicate the reference points for each subexpression (i.e., each
   paired parenthesis).  We refer to the value matched for subexpression
   <n> as $<n>.  For example, matching the above expression to
      http://www.ics.uci.edu/pub/ietf/uri/#Related
   results in the following subexpression matches:
      $1 = http:
      $2 = http
      $3 = //www.ics.uci.edu
      $4 = www.ics.uci.edu
      $5 = /pub/ietf/uri/
      $6 = <undefined>
      $7 = <undefined>
      $8 = #Related
      $9 = Related
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
不知道怎么发 PR 过来 https://gist.github.com/jsw0528/f343ee282bc1ea415f9f