Created
March 23, 2021 16:17
-
-
Save john-doherty/1dc4a0047035d599ac3819a17add4cbc to your computer and use it in GitHub Desktop.
Check if a string is a URL in JavaScript
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
/** | |
* Checks if a string is a URL | |
* @example isUrl('https://orcascan.com') // true; | |
* @param {string} str - value to test | |
* @returns {boolean} true if URL otherwise false | |
*/ | |
function isUrl(str) { | |
return /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment