Skip to content

Instantly share code, notes, and snippets.

@PseudoSky
Created September 26, 2016 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PseudoSky/850eaa5aec818bde6de5189c84d12b23 to your computer and use it in GitHub Desktop.
Save PseudoSky/850eaa5aec818bde6de5189c84d12b23 to your computer and use it in GitHub Desktop.
RegExp and utilities for grabbing parts of a path or url.
function getPathParts(path,i) {
return path.match(/^((http[s]?|ftp):\/)?\/?([^:\/\s]+)(:([^\/]*))?((\/[\w\/-]+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?$/i)[i];
}
// https://gist.github.com/url-matchers.js -> url-matchers.js
function getFileName(url){
return getPathParts(url, 8);
}
// https://gist.github.com/url-matchers.js -> gist.github.com
function getHost(url){
return getPathParts(url, 3);
}
// https://gist.github.com/url-matchers.js -> https
// http, https, ftp, etc...
function getScheme(url){
return getPathParts(url, 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment