0

I need to match https://example.com/ exactly but ignore any query strings so:

https://example.com/something should not match but https://example.com/?abc should match.

I've tried using https:\/\/www\.example\.com\/[^?]* but that doesn't seem to work and I'm struggling to find other suitable examples.

Apologies if this has been answered before, I've spent the last half an hour searching the site...

2 Answers 2

1

^https:\/\/example\.com(\/?\?.*)?$ will match

https://example.com/?abc
https://example.com?abc
https://example.com

but ignore

    https://example.com/something

Is that what you're looking for? Demo at https://regex101.com/r/B22n1p/1

Sign up to request clarification or add additional context in comments.

5 Comments

Appreciate you handling URLs that don't end in a trailing slash there too, thank you!
Actually I've just been testing this and realised that it doesn't match https://example.com/ on its own?
"ignore any query strings" is confusing. I've adapted my answer. My advice is next time to list clearly the strings you want to match and strings you want to not match with your regex.
Why do not include your own solution in the answer? ^https:\/\/example\.com(\/?\?.*)?$
Bug. Thanks for pointing out.
1

Don't forget that ? is an "optional quantifier".

I managed to get the desired with this expression.

(https:\/\/((www\.)|())example\.com)\/(([\?].*)|$)

Try it here for yourself.

2 Comments

Thanks for this. I'm not sure what you mean about ? being an optional qualifier? Alex beat you to it and their solution is a little bit easier to read so I've accepted their answer.
Actually I found an issue with their answer, I've accepted yours instead now 🙌

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.