What about:
^(?!\d+\.)(\w[\w\-]{0,24}\w)\.
-
(?!\d+\.)
is a negative lookahead. He checks that the domain is not just digits.
-
\w
after the first character is consistently a [\w\-]{0,24}
, there may be 0 or 24 letters, numbers, or hyphen.
-
não hífen
The last character can not also be a hyphen.
-
\w
start of the rest of the domain you are not interested in.
In \.
you expect (\w[\w\-]{0,24}\w)
+ 1 caracter não hífen
+ 0 até 24 letras, números ou hífen
. These three rules together guarantee that your domain will be at least 2 characters and a maximum of 26 characters.
You can see the regex running here .
Notes:
The above regex does not handle letters in other languages, as commented out by @Bacoo, because ECMAScript 5 and below do not provide native support for Unicode regexes ( ECMAScript 6 sim ).
The same regex could be implemented in PHP (which supports Unicode regexes) as follows:
^(?!\d+\.)([\p{L}0-9][\p{L}0-9\-]{0,24}[\p{L}0-9])\.
where 1 caracter não hífen
replaces [\p{L}0-9]
to accept any letters in any languages + digits. You can see this regex running here .