Created
December 15, 2014 18:02
-
-
Save dimorphic/c6fa589e51744d1abc89 to your computer and use it in GitHub Desktop.
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
/* Detects ftp | ftps | http | https and prepends http:// if none */ | |
function ensureUrlScheme(url) { | |
if (!/^(((f|ht)tps?)|chrome-extension):\/\//i.test(url)) { | |
url = "http://" + url; | |
} | |
return url; | |
} | |
/* Detects the scheme of the url, removes it */ | |
function detachUrlScheme(url) { | |
if (url.indexOf('https://') > -1) { | |
return url.replace('https://', ''); | |
} else if (url.indexOf('http://') > -1) { | |
return url.replace('http://', ''); | |
} else { | |
return null; | |
} | |
} | |
/* Ensure URL is valid */ | |
function isUrlValid(str) { | |
var pattern = new RegExp('^((https?:\\/\\/)?'+ // protocol | |
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name | |
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address | |
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path | |
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string | |
'(\\#[-a-z\\d_]*)?)|(chrome-extension://.*)$','i'); // fragment locator | |
if(!pattern.test(str)) { | |
return false; | |
} else { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment