Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
URL regular expression
//
// Regular Expression for URL validation
//
// Based on gist https://gist.github.com/729294 published by: Diego Perini
// Updated: 2012/11/02
//
// This validator differs from the Perini validator in a couple of ways:
// - It only allows http or https
// - It allows private network addresses
// - It does not allow query strings or fragments
// - It only allows the standard ASCII character set in names
//
//
var re_weburl = new RegExp(
"^" +
// protocol identifier
"(?:(?:https?)://)" +
"(?:" +
// IP address dotted notation octets
// excludes loopback network 0.0.0.0
// excludes reserved space >= 224.0.0.0
// excludes network & broacast addresses
// (first & last IP address of each class)
"(?:[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]))" +
"|" +
// host name
"(?:(?:[a-z0-9_]+-?)*[a-z0-9_]+)" +
// domain name
"(?:\\.(?:[a-z0-9]+-?)*[a-z0-9]+)*" +
// TLD identifier
"(?:\\.(?:[a-z]{2,}))" +
"|" +
// single host name
"(?:(?:[a-z0-9\\-_~]+))" +
")" +
// port number
"(?::\\d{2,5})?" +
// resource path
"(?:/[a-z0-9\\(\\)_/]*)?" +
"$", "i")
<html>
<head>
<title>URL Regex Tests</title>
<script src="regex-weburl.js"></script>
</head>
<body>
<script src="tests.js"></script>
</body>
</html>
//Unit tests for web url regular expression
//
// Many of the test URLs are from http://mathiasbynens.be/demo/url-regex, but their validity status is not
// always the same as in the source
//
var urls = {validUrls: ["http://foo.com/blah_blah",
"http://foo.com/blah_blah/",
"http://foo-bar.com/baz/quo/",
"http://foo.com/blah_blah_(wikipedia)",
"http://foo.com/blah_blah_(wikipedia)_(again)",
"http://142.42.1.1/",
"http://142.42.1.1/foo/bar/baz",
"http://142.42.1.1:8080/",
"http://142.42.1.1:8080/foo/bar/",
"http://223.255.255.254",
"http://a.b-c.de",
"https://foo.com",
"https://foobar",
"https://foo-bar",
"https://foo-bar.com",
"https://foo_bar",
"https://foo_bar.com",
"http://3628126748"
],
invalidUrls: ["http://",
"http://www.example.com/wpstyle/?p=364",
"http://userid:password@example.com:8080",
"http://foo.com/blah_(wikipedia)#cite-1",
"rdar://1234",
"http://.www.foo.bar/",
"http://www.foo.bar./",
"https://www.example.com/foo/?bar=baz&inga=42&quux",
"http://142.42.1.1:8080:30/",
"http://a.b--c.de/",
"http://code.google.com/events/#&product=browser",
"http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com",
"http:// shouldfail.com",
"http://-error-.invalid/",
"https://-foo.com",
"https://foo-.com",
"http://✪df.ws/123",
"http://➡.ws/䨹",
"http://900.900.900.900/",
"http://362812.34"
]
}
var evaluateUrls = function(matchExpected, testUrls, resultParent) {
for (var x = 0; x < testUrls.length; x++) {
var url = testUrls[x];
var result = url.match(re_weburl);
var resultDiv = document.createElement("div");
if (((result == null) && matchExpected) || ((result != null) && !matchExpected)) {
resultDiv.innerText = url + " FAILED!"
} else {
resultDiv.innerText = url + " Success"
}
resultParent.appendChild(resultDiv)
}
}
var testHeader = function(testLabel){var elem = document.createElement("h1"); elem.innerText = testLabel; return elem}
var validDiv = document.createElement("div");
document.body.appendChild(validDiv)
validDiv.appendChild(testHeader("test Valid URLs"));
evaluateUrls(true, urls.validUrls, validDiv)
var invalidDiv = document.createElement("div");
document.body.appendChild(invalidDiv)
invalidDiv.appendChild(testHeader("test invalid URLs"));
evaluateUrls(false, urls.invalidUrls, invalidDiv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.