User Preference 'Website' does not allow new TLDs

I have many questions about that RE.

Here’s a fragment to match a DNS label (other than top-level):

/[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?/i

What it is doing:

  • a label must start with a letter or number: [a-z0-9]
  • you can have letters, numbers and hyphens in the middle: [a-z0-9-]{0,61}
    • double hyphens are allowed
    • a label can be up to 63 characters long
  • a label must end with a letter or number: [a-z0-9]

The rules for a TLD are the same, except that all numeric is not allowed.

No number in first position (one character label okay): /([a-z]([a-z0-9-]{0,61}[a-z0-9])?)/i
No number in last position (two character minimum): /[a-z0-9][a-z0-9-]{0,61}[a-z]/i
At least one letter in middle part (hacky for making RE readable): /[a-z0-9][a-z0-9-]{1,30}[a-z]([a-z0-9-]{0,30}[a-z0-9])?/i

hacky

The hacky one could be rewritten to all compliant strings, but it is ugly to enforce a letter somewhere while also enforcing a length restriction. There will be a ton of |s in the proper answer.

Combined hostname RE: (labelpart\.)+(tldpart1|tldpart2|tldpart3) :

/([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+(([a-z]([a-z0-9-]{0,61}[a-z0-9])?)|[a-z0-9][a-z0-9-]{0,61}[a-z]|[a-z0-9][a-z0-9-]{1,30}[a-z]([a-z0-9-]{0,30}[a-z0-9])?/i

And final combined WEBSITE_REGEXP:

WEBSITE_REGEXP = /(^$)|(^(http|https):\/\/([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+(([a-z]([a-z0-9-]{0,61}[a-z0-9])?)|[a-z0-9][a-z0-9-]{0,61}[a-z]|[a-z0-9][a-z0-9-]{1,30}[a-z]([a-z0-9-]{0,30}[a-z0-9])?\/.*)?$)/ix

(The syntax highlighter gets confused by the “Combined” line.)

Does /x to ruby mean the same as /x to perl? If so, in-line comments and a reformat should be added.

2 Likes