Skip to content

Latest commit

 

History

History
62 lines (38 loc) · 3.35 KB

regex.md

File metadata and controls

62 lines (38 loc) · 3.35 KB

Etc / Good Enough™ regexes

URLs

  • Optional port: (?::\d{1,5})?
  • Optional http scheme: (?:http(?:s)?:\/\/)?

Development URLS

^(?:http(?:s)?:\/\/)?(?:localhost|127\.0\.0\.1|0\.0\.0\.0)(?::\d{1,5})?$

https://regex101.com/r/accq7h/4/tests

Popular deployment options

^(?:http(?:s)?:\/\/)?[a-zA-Z0-9-]{1,63}\.gitlab\.io$

^(?:http(?:s)?:\/\/)?[a-zA-Z0-9-]{1,63}\.github\.io$

^(?:http(?:s)?:\/\/)?[a-zA-Z0-9-]{1,63}\.herokuapp\.com$

Domains

There're two approaches to choose from when validating domains.

By-the-books FQDN matching (theoretical definition, rarely encountered in practice):

Practical / conservative FQDN matching (practical definition, expected and supported in practice):

  • by-the-books matching with the following exceptions/additions
  • valid characters: [a-zA-Z0-9.-]
  • labels cannot start or end with hyphens (as per RFC-952 and RFC-1123/2.1)
  • TLD min length is 2 character, max length is 24 character as per currently existing records
  • don't match trailing dot

Regex for the practical use case:

^(?!.*?_.*?)(?!(?:[\w]+?\.)?\-[\w\.\-]*?)(?![\w]+?\-\.(?:[\w\.\-]+?))(?=[\w])(?=[\w\.\-]*?\.+[\w\.\-]*?)(?![\w\.\-]{254})(?!(?:\.?[\w\-\.]*?[\w\-]{64,}\.)+?)[\w\.\-]+?(?<![\w\-\.]*?\.[\d]+?)(?<=[\w\-]{2,})(?<![\w\-]{25})$

https://regex101.com/r/FLA9Bv/41 (Note: currently only works in Chrome because the regex uses lookbehinds which are only supported in ECMA2018)

See also: TLD limitations, domain limitations, list of TLDs, by-the-books regex and explanation

Semantic version

^v[0-9]+\.[0-9]+\.[0-9]+$

Argument list

Extracts valid arguments from comma-separated argument list, supporting double-quoted arguments

(?<=")[^"]+?(?="(?:\s*?,|\s*?$))|(?<=(?:^|,)\s*?)(?:[^,"\s][^,"]*[^,"\s])|(?:[^,"\s])(?![^"]*?"(?:\s*?,|\s*?$))(?=\s*?(?:,|$))

https://regex101.com/r/UL8kyy/3/tests (Note: currently only works in Chrome because the regex uses lookbehinds which are only supported in ECMA2018)