Skip to content

Instantly share code, notes, and snippets.

@geedew
Forked from dperini/regex-weburl.js
Created November 5, 2013 20:26
Show Gist options
  • Save geedew/7325638 to your computer and use it in GitHub Desktop.
Save geedew/7325638 to your computer and use it in GitHub Desktop.
//
// Regular Expression for URL validation
//
// Author: Diego Perini
// Updated: 2010/12/05
//
// the regular expression composed & commented
// could be easily tweaked for RFC compliance,
// it was expressly modified to fit & satisfy
// these test for an URL shortener:
//
// http://mathiasbynens.be/demo/url-regex
//
// Notes on possible differences from a standard/generic validation:
//
// - utf-8 char class take in consideration the full Unicode range
// - TLDs have been made mandatory so single names like "localhost" fails
// - protocols have been restricted to ftp, http and https only as requested
//
// Compressed one-line version (228 characters):
//
// var re_weburl = /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\u00a1-\uffff]+-?)*[a-z\d\u00a1-\uffff]+)(?:\.(?:[a-z\d\u00a1-\uffff]+-?)*[a-z\d\u00a1-\uffff]+)*(?:\.[a-z\u00a1-\uffff]{2,6}))(?::\d+)?(?:[^\s]*)?$/i;
//
var re_weburl = new RegExp(
"^" +
// protocol identifier
"(?:(?:https?|ftp)://)" +
"(?:" +
// user:pass@ authentication
"\\S+(?::\\S*)?@|" +
// IP dotted octets notation
"\\d{1,3}(?:\\.\\d{1,3}){3}|" +
// host name part
"(?:(?:[a-z\\d\\u00a1-\\uffff]+-?)*[a-z\\d\\u00a1-\\uffff]+)" +
// domain name part
"(?:\\.(?:[a-z\\d\\u00a1-\\uffff]+-?)*[a-z\\d\\u00a1-\\uffff]+)*" +
// TLD identifier part
"(?:\\.[a-z\\u00a1-\\uffff]{2,6})" +
")" +
// port number
"(?::\\d+)?" +
// resource path
"(?:[^\\s]*)?" +
"$", "i"
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment