Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
195
rated 0 times [  199] [ 4]  / answers: 1 / hits: 21033  / 11 Years ago, thu, may 9, 2013, 12:00:00

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.


More From » regex

 Answers
8

Try



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

[#78330] Wednesday, May 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ross

Total Points: 477
Total Questions: 97
Total Answers: 98

Location: France
Member since Thu, May 6, 2021
3 Years ago
;