Skip to content

Instantly share code, notes, and snippets.

@buren
Created June 23, 2018 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buren/d25015abec352a31f517edceb2c5b094 to your computer and use it in GitHub Desktop.
Save buren/d25015abec352a31f517edceb2c5b094 to your computer and use it in GitHub Desktop.
Match valid subdomain string.
SUBDOMAIN_MATCHER = /\A([a-z][a-z\d]*(-[a-z\d]+)*|xn--[\-a-z\d]+)\z/i
[
['as a', nil],
['a-a', 0],
['todd2', 0],
['todd-2', 0],
['todd-admin', 0],
['_todd-admin', nil],
['-todd-admin', nil],
['2todd-2', nil],
['todd_admin', nil],
['a_a', nil]
].map do |data|
subdomain, expected = data
(subdomain =~ SUBDOMAIN_MATCHER) == expected
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment