parseUri 1.2: Split URLs in JavaScript


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // parseUri 1.2.2
  2. // (c) Steven Levithan <stevenlevithan.com>
  3. // MIT License
  4.  
  5. function parseUri (str) {
  6. var o = parseUri.options,
  7. m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
  8. uri = {},
  9. i = 14;
  10.  
  11. while (i--) uri[o.key[i]] = m[i] || "";
  12.  
  13. uri[o.q.name] = {};
  14. uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
  15. if ($1) uri[o.q.name][$1] = $2;
  16. });
  17.  
  18. return uri;
  19. };
  20.  
  21. parseUri.options = {
  22. strictMode: false,
  23. key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
  24. q: {
  25. name: "queryKey",
  26. parser: /(?:^|&)([^&=]*)=?([^&]*)/g
  27. },
  28. parser: {
  29. strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
  30. loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
  31. }
  32. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.