Skip to content

Instantly share code, notes, and snippets.

@PSingletary
Forked from jeremieflrnt/regex.ts
Created February 20, 2025 11:31
Show Gist options
  • Save PSingletary/6e77bcf85b62ad77cf6208b39455c5af to your computer and use it in GitHub Desktop.
Save PSingletary/6e77bcf85b62ad77cf6208b39455c5af to your computer and use it in GitHub Desktop.
Ultimate regex specific domain
export const getValidUrlRegex = (domainName?: string) => {
return domainName
? RegExp(
/^(?:https?:\/\/)?(?:www\.)?(?:(?<subdomain>[a-z0-9-]+)\.)?/.source +
RegExp(domainName).source +
/\.(?:(?<tld>[a-z]{1,63}))?(?::(?<port>[0-9]{1,5}))??(?:[/#](?<path>[\w/\-._~:/?#[\]@!$&'()*+,;=.]*)?)?$/
.source,
)
: /^(?:https?:\/\/)?(?:www\.)?(?:(?<subdomain>[a-z0-9-]+)\.)?(?<domain>[a-z0-9-]+)\.(?:(?<tld>[a-z]{1,63}))?(?::(?<port>[0-9]{1,5}))??(?:[/#](?<path>[\w/\-._~:/?#[\]@!$&'()*+,;=.]*)?)?$/;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment