Skip to content

Instantly share code, notes, and snippets.

@JonathanGawrych
Created February 8, 2024 22:39
Show Gist options
  • Select an option

  • Save JonathanGawrych/0bb92a46ca99aed670839feab32f18b9 to your computer and use it in GitHub Desktop.

Select an option

Save JonathanGawrych/0bb92a46ca99aed670839feab32f18b9 to your computer and use it in GitHub Desktop.
Ensure that a well-formed url matches a domain and it's subdomains, but don't allow userInfo
// Modified from https://github.com/google/closure-library/blob/7818ff7dc0b53555a7fb3c3427e6761e88bde3a2/closure/goog/uri/utils.js#L189-L208
new RegExp(
'^' + // Anchor against the entire string.
'https?:\\/\\/' + // scheme
'(?![^\\\\\\/?#]*@)' + // userInfo disallowed
'(?:(?:[^\\\\\\/?#\\.]+?)\\.)*' + // subdomain
'mydomain.com' + // domain
'(?::[0-9]+)?' + // port
'(?=[\\\\\\/?#]|$)' // authority-terminating character.
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment