18

I'm looking for a regex that accept urls like these:

http://www.example.com
www.example.com

This is what I have so far, but it regex doesn't match URLs without http:// or https://, or ftp://:

regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;

How can I make the protocol optional?

Nic
  • 6,211
  • 10
  • 46
  • 69
Guest
  • 207
  • 2
  • 3
  • 7
  • 1
    `example.pl`: A website in Poland or a Perl script? – Boldewyn Nov 25 '10 at 09:44
  • In the OP's case unfortunately not. It will either be consumed by the regex or not. [RFC 3986](http://labs.apache.org/webarch/uri/rfc/rfc3986.html#regexp) (the link leads to a regexp to parse URLs) has its reasons to enforce either a scheme or the `\\` part. Although I understand the need to find such stuff (see [the Linkification addon for FF](http://yellow5.us/firefox/linkification/)) – Boldewyn Nov 25 '10 at 09:55
  • Also see http://stackoverflow.com/questions/4252778/put-urls-from-string-into-array-using-regex-problem-with-trailing-period/4252806#4252806 for a very generic solution. – martineno Nov 25 '10 at 10:04
  • [What is a good regular expression to match a URL?](https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url) – Rajat Sep 07 '21 at 11:40
  • https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url – Rajat Sep 07 '21 at 11:40

6 Answers6

15

Make the (ftp|http|https):\/\/ part optional:

((ftp|http|https):\/\/)?
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • 1
    Thank you but now I can match for example "example" or "example@example.com". And I don't want it. So how can I solve this? My regex: regexp = /((ftp|http|https):\/\/)?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; I dont't want to match "example@example.com" or "example" – Guest Nov 25 '10 at 10:18
  • `example@example.com` is actually a valid URL: The first `example` is a username, that can be submitted this way. Try loading this very page with an added `example@` between `http://` and `stackoverflow.com`. – Boldewyn Nov 25 '10 at 11:30
  • I don't want to accept @ in my urls. Do you know how I can change my regex? – Guest Nov 26 '10 at 08:29
4

Try this this will validate url with (http,ftp,https) or without(http,ftp,https)..

/^(?:(ftp|http|https):\/\/)?(?:[\w-]+\.)+[a-z]{3,6}$/;
  • 3
    This regex do not accept url for domains from germany. e.g (www.germany.de). You have to change the regex to `/^(?:(ftp|http|https):\/\/)?(?:[\w-]+\.)+[a-z]{2,6}$/;` The 2 is important. – Simon Ludwig Jun 01 '16 at 23:19
  • 2
    This is not such a great way to do this. Really all you can do is check for a `.`. There are a lot of new TLDs with names longer than 6 chars... – Askdesigners Sep 13 '16 at 14:46
  • TLDs can be between 2 and (currently the longest TLD is) 24 characters long (https://stackoverflow.com/questions/9238640/how-long-can-a-tld-possibly-be/9239264). `/^(?:(ftp|http|https):\/\/)?(?:[\w-]+\.)+[a-z]{2,24}$/` – Rillus Jan 05 '22 at 12:07
3

Try this this will validate url with or without(http,ftp,https) in upper and lower cases and also allows you to for numerics

/^(?:(ftp|http|https)?:\/\/)?(?:[\w-]+\.)+([a-z]|[A-Z]|[0-9]){2,6}$/gi;
1

Kindly see https://codegolf.stackexchange.com/a/480/6593

Quoting from the above link:

value = 'www.google.com';
if(/(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi.test(value)) {
            return true;
        } else {
            return false;
        }
Community
  • 1
  • 1
1

regex accept url without http or https

[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@%_\+.~#?&//=]*)

In HTML5

<input pattern="[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@%_\+.~#?&//=]*)" type="text>
Ali Raza
  • 670
  • 5
  • 15
0

You can use the next reg: ^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$

Here is an example: https://www.regextester.com/93652

Ruslan Korkin
  • 3,973
  • 1
  • 27
  • 23