Skip to content

Instantly share code, notes, and snippets.

@jeremieflrnt
Created August 9, 2022 14:54
Show Gist options
  • Select an option

  • Save jeremieflrnt/03821efd292128f2d60213b38e8ebf23 to your computer and use it in GitHub Desktop.

Select an option

Save jeremieflrnt/03821efd292128f2d60213b38e8ebf23 to your computer and use it in GitHub Desktop.

Revisions

  1. jeremieflrnt created this gist Aug 9, 2022.
    10 changes: 10 additions & 0 deletions regex.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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/\-._~:/?#[\]@!$&'()*+,;=.]*)?)?$/;
    };