Skip to content

Instantly share code, notes, and snippets.

@dmyersturnbull
Last active February 13, 2026 01:09
Show Gist options
  • Select an option

  • Save dmyersturnbull/c147e8d70a27f0d0f91f584776ade85e to your computer and use it in GitHub Desktop.

Select an option

Save dmyersturnbull/c147e8d70a27f0d0f91f584776ade85e to your computer and use it in GitHub Desktop.
Regex for canonical public-facing URIs. Specifically, matches lowercase, ASCII (or Punycode-encoded) RFC 3986 URIs with DNS hostnames and no credentials. Per spec, allows empty path segments; unencoded `:`, `@`, `/`, and `?` selectively; and unencoded sub-delimiters. Dialect is ECMA-262 2025 with whitespace.
^
(?= .{1,255}+ (?:$|/) )
(?! \d{1,3}(?:\.\d{1,3}){3} ($|/) )
(?<host>
[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?
(?: \. [a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])? )+
)
(?<path>
/ [-\w.~!$'()*+:@;,&=]*
)?
(?<query>
\? [-\w.~!$'()*+:@;,&=?]*
)?
(?<fragment>
# [-\w.~!$'()*+:@;,&=?]*
)?
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment