Home > front end >  Regex Match URL Without Query String
Regex Match URL Without Query String

Time:07-13

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...

CodePudding user response:

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.

CodePudding user response:

https:\/\/example\.com\/?\?.* will match

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

but ignore

    https://example.com/something

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

  • Related