Skip to content

Instantly share code, notes, and snippets.

@jeremieflrnt
Created August 9, 2022 14:54
Show Gist options
  • Save jeremieflrnt/03821efd292128f2d60213b38e8ebf23 to your computer and use it in GitHub Desktop.
Save jeremieflrnt/03821efd292128f2d60213b38e8ebf23 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