Skip to content

Instantly share code, notes, and snippets.

@rodneyrehm
Created December 17, 2013 21:41
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rodneyrehm/8013067 to your computer and use it in GitHub Desktop.
Save rodneyrehm/8013067 to your computer and use it in GitHub Desktop.
JavaScript - URL validation RegExp
// see http://rodneyrehm.de/t/url-regex.html#imme_emosol for the complete table
// expression by
var url_pattern = /^(https?|ftp|torrent|image|irc):\/\/(-\.)?([^\s\/?\.#-]+\.?)+(\/[^\s]*)?$/i;
var valid = [
"http://foo.com/blah_blah",
"http://foo.com/blah_blah/",
"http://foo.com/blah_blah_(wikipedia)",
"http://foo.com/blah_blah_(wikipedia)_(again)",
"http://www.example.com/wpstyle/?p=364",
"https://www.example.com/foo/?bar=baz&inga=42&quux",
"http://✪df.ws/123",
"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://192.168.1.1/",
"http://192.168.1.1:8080/",
"http://➡.ws/䨹",
"http://⌘.ws",
"http://⌘.ws/",
"http://foo.com/blah_(wikipedia)#cite-1",
"http://foo.com/blah_(wikipedia)_blah#cite-1",
"http://foo.com/unicode_(✪)_in_parens",
"http://foo.com/(something)?after=parens",
"http://☺.damowmow.com/",
"http://code.example.com/events/#&product=browser",
"http://j.mp",
"ftp://foo.bar/baz",
"torrent://foo.bar/baz",
"image://foo.bar:993",
"irc://foo.bar:6667"
];
var invalid = [
"rdar://1234",
"http://",
"http://.",
"http://..",
"http://../",
"http://?",
"http://??",
"http://??/",
"http://#",
"http://##",
"http://##/",
"http://foo.bar?q=Spaces should be encoded",
"//",
"//a",
"///a",
"///",
"http:///a",
"foo.com",
"http://-a.b.co",
"http://a.b-.co"
];
valid.forEach(function(text) {
if (!text.match(url_pattern)) {
console.error("failed to match", text);
}
});
invalid.forEach(function(text) {
if (text.match(url_pattern)) {
console.error("failed to not match", text);
}
});
@dersonsena
Copy link

https://www.foo-bar.com doesn't work and this is a valid URL such as https://material-ui.com

@AbhilashDawar
Copy link

Removing the - from [^\s\/?\.#-] section of the regex works for the scenarios mentioned above by @dersonsena. But then it also starts working for two or more hyphens (-) together (might need to enhance this case.

Updated regex after removing hyphen var url_pattern = /^(https?|ftp|torrent|image|irc):\/\/(-\.)?([^\s\/?\.#]+\.?)+(\/[^\s]*)?$/i;

@dersonsena
Copy link

Nice! =)

@rajan-chavda
Copy link

Thanks. I have referred a lot of docs for this.

@galek
Copy link

galek commented Dec 2, 2020

Hello, your regular is revert incorrect result for correct url:
http://y-a.ru

@meowzin644
Copy link

It doesn't work for: https://off---white.com which has several hyphens. such a weird URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment