Skip to content

Instantly share code, notes, and snippets.

@john-doherty
Created March 23, 2021 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save john-doherty/1dc4a0047035d599ac3819a17add4cbc to your computer and use it in GitHub Desktop.
Save john-doherty/1dc4a0047035d599ac3819a17add4cbc to your computer and use it in GitHub Desktop.
Check if a string is a URL in JavaScript
/**
* 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