Created
August 9, 2022 14:54
-
-
Save jeremieflrnt/03821efd292128f2d60213b38e8ebf23 to your computer and use it in GitHub Desktop.
Ultimate regex specific domain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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