I would like to validate a hostname using only regualr expression.
Host Names (or \'labels\' in DNS jargon) were traditionally defined by RFC 952 and RFC 1123 and m
A revised regex based on comments here and my own reading of RFCs 1035 & 1123:
Ruby: \A(?!-)[a-zA-Z0-9-]{1,63}(? (tests below)
Python: ^(?!-)[a-zA-Z0-9-]{1,63}(? (not tested by me)
Javascript: pattern = /^(?!-)[a-zA-Z0-9-]{1,63}$/g; (based on Tom Lime's answer, not tested by me)
tests = [
['01010', true],
['abc', true],
['A0c', true],
['A0c-', false],
['-A0c', false],
['A-0c', true],
['o123456701234567012345670123456701234567012345670123456701234567', false],
['o12345670123456701234567012345670123456701234567012345670123456', true],
['', false],
['a', true],
['0--0', true],
["A0c\nA0c", false]
]
regex = /\A(?!-)[a-zA-Z0-9-]{1,63}(?