Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <script src="http://cdn.rawgit.com/HenkPoley/8899766/raw/6f06f60c4d9d779dd1428b67bf9fa5025c9ba354/regex-weburl.js"></script>
</head>
<body>
  
</body>
</html>
 
//Unit tests for web url regular expression
//
// It contains all of the test URLs are from http://mathiasbynens.be/demo/url-regex
//
 
var urls = {validUrls: [
 "ftp://foo.bar/baz",
 "http://1337.net",
 "http://foo.bar/?q=Test%20URL-encoded%20stuff",
 "http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com",
 "http://142.42.1.1",
 "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/",
 "http://142.42.1.1:8080/foo/bar/",
 "http://223.255.255.254",
  
  
 "http://2915201185/search?q=hello", // Google
 "http://3628126748", // Some server from Coca Cola brazil
 
 "http://3628126748.com",
 "http://a.b-c.de",
 "http://a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.com",
 "http://code.google.com/events/#&product=browser",
 "http://j.mp",
 "http://example.com/index.html",
 "http://foo-bar.com/baz/quo/",
 "http://foo.com/blah_(wikipedia)#cite-1",
 "http://foo.com/blah_(wikipedia)_blah#cite-1",
 "http://foo.com/unicode_(✪)_in_parens",
 "http://foo.com/blah_blah",
 "http://foo.com/blah_blah/",
 "http://foo.com/blah_blah_(wikipedia)",
 "http://foo.com/blah_blah_(wikipedia)_(again)",
 "http://foo.com/(something)?after=parens",
 "http://☺.damowmow.com/",
 "http://google.com:80/",
 "http://google3.com",
 "http://mw1.google.com/mw-earth-vectordb/kml-samples/gp/seattle/gigapxl/$[level]/r$[y]_c$[x].jpg",
 "http://nic.xn--unup4y",
 "http://nic.游戏",
 "http://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body",
 "http://userid:password@example.com:8080",
 "http://userid:password@example.com:8080/",
 "http://userid@example.com",
 "http://userid@example.com/",
 "http://userid@example.com:8080",
 "http://userid@example.com:8080/",
 "http://userid:password@example.com",
 "http://userid:password@example.com/",
 "http://usern%40me:password@example.com/",
 "http://username:password@example.com/",
 "http://www.example.com/wpstyle/?p=364",
 "http://www.example.xn--really-long-punycode-test-string-test-tests-123-tests-tests/",
 "http://www.microsoft.xn--comindex-g03d.html.irongeek.com/", // YUCK!
 "http://xn--h32b13vza.xn--3e0b707e/",
 "http://xn—y3h.tk/",
 "http://[1080:0:0:0:8:800:200C:417A]/index.html",
 "http://[1080::8:800:200C:417A]/foo",
 "http://[2010:836B:4179::836B:4179]",
 "http://[3ffe:2a00:100:7031::1]",
 "http://[::192.9.5.5]/ipng",
 "http://[::FFFF:129.144.52.38]:80/index.html",
 "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html",
 "http://مثال.إختبار",
 "http://例子.测试",
 "http://उदाहरण.परीक्षा",
 "http://✪df.ws/123",
 "http://➡.ws/䨹",
 "http://⌘.ws",
 "http://⌘.ws/",
 "https://asd@xn----7sbbtkohtqhvkc8j.xn--p1ai",
 "https://foo-bar.com",
 "https://foo.com",
 "https://localhost/",
 "https://www.example.com/foo/?bar=baz&inga=42&quux",
 "http://ABC.com/%7Esmith/home.html",
 ],
 
invalidUrls: [
 "//",
 "///",
 "///a",
 "//a",
 ":// should fail",
 "foo.com",
 "ftps://foo.bar/",
 "h://test",
 "htt://google.com",
 "http:// shouldfail.com",
 "http://",
 "http://#",
 "http://##",
 "http://##/",
 "http://-a.b.co",
 "http://-error-.invalid/",
 "http://.",
 "http://..",
 "http://../",
 "http://.www.foo.bar./",
 "http://.www.foo.bar/",
 "http:///a",
 "http://0.0.0.0",
 "http://1.1.1.1.1",
 "http://10.1.1.0",
 "http://10.1.1.1",
 "http://10.1.1.254",
 "http://10.1.1.255",
 "http://123.123.123",
 "http://142.42.1.1:8080:30/",
 "http://224.1.1.1",
 "http://362812.34",
 "http://3628126748", // Technically valid though
 "http://900.900.900.900/",
 "http://?",
 "http://??",
 "http://??/",
 "http://?example.?om/",
 "http://@",
 "http://@a.ch/",
 "http://a@@a.ch/",
 "http://a@a@a.ch/",
  
 "http://a.b--c.de/",
 "http://a.b-.co",
 "http://abc..com/",
 "http://foo.bar/foo(bar)baz quux",
 "http://foo.bar?q=Spaces should be encoded",
 "http://foo_bar.com",
 "http://foo_bar.google.com",
 "http://go/ogle.com",
 "http://google.com/ /",
 "http://google\\.com",
 "http://www(google.com",
 "http://www.example.xn--overly-long-punycode-test-string-test-tests-123-test-test123/",
 "http://www.foo.bar./",
 "http://www=google.com",
 "https://-foo.com",
 "https://foo-.com",
 "https://foo_bar",
 "https://www.g.com/error\n/bleh/bleh",
 "rdar://1234",
 
]
}
 
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.innerHTML = '<b style="color:red;">FAIL</b> ' + url
   } else {
     resultDiv.innerHTML = '<b style="color:green;">PASS</b> ' + url
   }
   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)
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers