Skip to content

Instantly share code, notes, and snippets.

@asabaylus
Created May 1, 2015 17:46
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 asabaylus/6e22452b2efa02fcf197 to your computer and use it in GitHub Desktop.
Save asabaylus/6e22452b2efa02fcf197 to your computer and use it in GitHub Desktop.
isUrl
_isUrl = function (str) {
// pass --> http://google.com
// pass --> ftp://google.com
// pass --> google.com
// pass --> localhost
// pass --> 127.0.0.1
// pass --> 1.1.1.1
// pass --> http://127.0.0.1
// pass --> http://localhost
// fail --> foo
// fail --> http://foo
// pass --> http://
// fail --> http:dfsdgfg.com
// pass --> a.io
//
// Author: Diego Perini
// Updated: 2010/12/05
// License: MIT
//
// Copyright (c) 2010-2013 Diego Perini (http://www.iport.it)
// https://gist.github.com/dperini/729294
// modified by Asa Baylus to accomodate a wider range of URLs
if (typeof str === 'undefined') {
return new Error('_isUrl: no arguments provided');
}
return /^(?:(?:https?|ftp|):\/\/)?(localhost|127\.0\.0\.1)?(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)?(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))?(?::\d{2,5})?(?:\/[^\s]*)?$/i.test(str);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment