Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex: Allowlisting URL Top-Level-Domains

I’m looking for a regex that allowlists specific TLDs in the URL scheme. Most of my tests are passing except for ones that repeat the TLD:

Regex

^https:\/\/[^\/]+\.my-site\.com|cloud\/?.*?$

False Positive

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

https://qa.my-site.cloud.cloud/check-this-out?check-it-out=true

This is showing as a valid match according to the regex. How do I avoid the regex matching URLs with repeated TLDs? Adding a group + {1} did not solve the problem: ^https:\/\/[^\/]+\.my-site\.(?:com|cloud){1}\/?.*?$

Language being used is Javascript.

>Solution :

There are two issues:

  1. You need to wrap alternation (|) in parentheses: (com|cloud) instead of com|cloud
  2. The / should be required if there is a path to the URL

Here’s a working regex:

^https:\/\/[^\/]+\.my-site\.(com|cloud)(\/.*?)?$

Try it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading