mannykary
mannykary

Reputation: 832

Regex for URL matching that ignores query parameters

I am trying to come up with a regular expression that matches the following URLs:

http://test.com/bobs/your/uncle/1
http://test.com/bobs/your/uncle/5
http://test.com/bobs/your/uncle/5?tab=1

But does not match the following URLs:

http://test.com/bobs/your/uncle/1/brah/5
http://test.com/bobs/your/uncle/1/brah/5?tab=2

I tried the following regex

/bobs/your/uncle/\d+\??

Which works for the first three URLs above, but not the last two.

Upvotes: 1

Views: 4856

Answers (2)

Kasravnd
Kasravnd

Reputation: 107297

You need to use start and end anchors :

'bobs/your/uncle/\d+\??[^/]*$'

See demo https://regex101.com/r/pP2fL1/1

you can use [^/]* to match some extra strings like tab=1 which match any thing except /.

Upvotes: 3

anubhava
anubhava

Reputation: 785276

You can use this regex:

/bobs/your/uncle/\d+(?:\?|$)

(?:\?|$) will match ? or end of input after your desired input URIs.

Upvotes: 4

Related Questions