domain-regex icon indicating copy to clipboard operation
domain-regex copied to clipboard

Hostname with underscores

Open riteable opened this issue 9 years ago • 2 comments

domain-_name.com is correctly invalidated, but domain_-name.com is not.

riteable avatar Aug 08 '16 11:08 riteable

bump. (:

derhuerst avatar Oct 21 '16 18:10 derhuerst

The issue is actually with hostnames including hyphen. \b will break on a hyphen, so in your example domain_-name.com, while the entire string is invalid, it is only matching name.com as \b matches -. Extracting the substring and using beginning and end of string anchors \A\Z will force it to evaluate the entire sequence:

\A((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\Z

Or you can use lookahead/lookbehind assertions with whitespace instead of \b, like so:

(?<=\s|\A)((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}(?=\s|\Z)

brimston3 avatar Dec 16 '16 16:12 brimston3