Created
September 26, 2016 00:59
-
-
Save PseudoSky/850eaa5aec818bde6de5189c84d12b23 to your computer and use it in GitHub Desktop.
RegExp and utilities for grabbing parts of a path or url.
This file contains 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
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