7

I haven't been able to find this one and what I'm trying isn't quite working out for me.

I want to match only domains that:

  • don't contain a protocol (http, https, ftp)
  • optionally include a subdomain
  • don't start with a hyphen but can contain a hyphen

Example domains that would match:

  • domain.com
  • example.domain.com
  • example.domain-hyphen.com
  • www.domain.com
  • example.museum

Example domains that would not match:

  • http://example.com
  • subdomain.-example.com
  • example.com/parameter
  • example.com?anything
  • www.subdomain.domain.com

What I've currently got:

/^(?!:\/\/)(^[a-zA-Z0-9])?.[a-zA-Z0-9-]+\.[a-zA-Z]{2,6}?$/i

It's not matching protocols, allowing hyphen inside the domain, not allowing trailing characters after the TLD, and is allowing a subdomain (but only 1 character).

I still need to allow subdomains of any length, not allow www.subdomain.domain.com and not allow a leading hyphen.

dda
  • 6,030
  • 2
  • 25
  • 34
Jared Eitnier
  • 7,012
  • 12
  • 68
  • 123

4 Answers4

15

Try

/^(?!:\/\/)([a-zA-Z0-9]+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+\.[a-zA-Z]{2,6}?$/i
dda
  • 6,030
  • 2
  • 25
  • 34
  • 10
    I know this is old, but it should be updated to allow domains such as .photography, which has more than 6 characters – Nico Aug 25 '14 at 15:34
  • 1
    @dda I see that you have deleted your TSE profile (I came to know after losing 310 points). Although I don't have any right to ask, I hope everything is well. That forum is boring without your edits. – Hanky Panky Sep 18 '18 at 08:37
  • @HankyPanky One of my answers was edited, against, my wish, and locked, to prevent to change it back. It was the last straw of a series of things that made me regret joining TSE in the first place. It became obvious my work wasn't appreciated by the moderators... – dda Sep 19 '18 at 12:18
  • 1
    I can't say anything on behalf of the moderators then but I have always found your contribution valuable. I used to think you'd make a great moderator one day (I still think so). I'm disappointed to see you go and I hope you guys sort it out and be back. – Hanky Panky Sep 19 '18 at 19:23
  • @HankyPanky Also, right after I left, there was an attempt at downvoting me 27 times in a row on Expats (where I am no longer active either). This was quickly overturned, but it obviously was a TSE member who was unhappy with the points loss. I also on chat that the person who censored my answer was unhappy about my leaving. Should I thought about his actions first. But I also saw that not everybody was unhappy... So, farewell. – dda Sep 25 '18 at 07:07
  • 1
    I don’t know why TSE has become like that lately but then it’s such a big crowd that I can’t ever speak for someone. I will definitely miss your contribution and I hope you do come back. A few people not doing the right thing don’t really represent the whole community. – Hanky Panky Sep 25 '18 at 18:25
  • doesn't work with new domains, even something as simple as inte.io failed – Neo Nov 27 '22 at 13:02
4

Let's analyse your regex:

^(?!:\/\/)

This is pretty useless. While it indicates the intention of the regex, it's unnecessary since the following characters are not allowed to contain slashes anyway.

(^[a-zA-Z0-9])?.

I think you wanted this to be ^([a-zA-Z0-9]+\.)?. Your dot is not escaped, and would be preceded by only one optional character at the string beginning.

[a-zA-Z0-9-]+

If you want this not to begin with a hyphen, you either could use a negative lookahead or better just [a-zA-Z0-9][a-zA-Z0-9-]*.

\.[a-zA-Z]{2,6}?

Not sure what the question mark does here. There's no backtracking anyway?

/i

This renders the explicit [a-zA-Z] useless, one would be enough. Or omit the i flag.

All these things together, we will end up with

/^([a-z0-9]+\.)?[a-z0-9][a-z0-9-]*\.[a-z]{2,6}$/i
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 2
    I know this is old, but it should be updated to allow domains such as .photography, which has more than 6 characters – Nico Aug 25 '14 at 15:35
  • Sure, however I've only analyzed the regex that was presented in the question and got it working (without commenting on the meaning) – Bergi Aug 25 '14 at 15:41
  • 1
    Yes, you're right, sorry... But when I saw it I thought if people hunt for the answer they might copy this one without even considering its limitations... So leave it as is – Nico Aug 26 '14 at 11:39
4

Try this:

^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$

Demo

Dharmesh Hadiyal
  • 719
  • 1
  • 7
  • 18
1

The RegEx I came up with while doing the Hostname/FQDN validation in Javascript:

FQDN:

^(?!:\/\/)(?!.{256,})(([a-z0-9][a-z0-9_-]*?\.)+?[a-z]{2,6}?)$/i

Hostname or FQDN

^(?!:\/\/)(?!.{256,})(([a-z0-9][a-z0-9_-]*?)|([a-z0-9][a-z0-9_-]*?\.)+?[a-z]{2,6}?)$/i

Both expressions use lookahead to check the total string length, which can be up to 255 characters. They also do a lazy check .{x,y}?.

Note that it is using case insensitive match /i.

dda
  • 6,030
  • 2
  • 25
  • 34