Hostname with underscores
domain-_name.com is correctly invalidated, but domain_-name.com is not.
bump. (:
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)