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
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:
- You need to wrap alternation (
|) in parentheses:(com|cloud)instead ofcom|cloud - The
/should be required if there is a path to the URL
Here’s a working regex:
^https:\/\/[^\/]+\.my-site\.(com|cloud)(\/.*?)?$