Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match Url path without query string

I would like to match a path in a Url, but ignoring the querystring. The regex should include an optional trailing slash before the querystring.

Example urls that should give a valid match:

/path/?a=123&b=123

/path?a=123&b=123

So the string '/path' should match either of the above urls.

I have tried the following regex: (/path[^?]+).*

But this will only match urls like the first example above: /path/?a=123&b=123

Any idea how i would go about getting it to match the second example without the trailing slash as well?

Regex is a requirement.

like image 360
JCoder23 Avatar asked Oct 27 '13 21:10

JCoder23


People also ask

Does URL include query string?

A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters.

What is the difference between the path and a query string in a URL?

The first difference between query and path parameters is their position in the URL. While the query parameters appear on the right side of the '? ' in the URL, path parameters come before the question mark sign. Secondly, the query parameters are used to sort/filter resources.

Are URL parameters always strings?

Yes, URL query string params are of type string. It's up to you to convert them to and from the type you need.


1 Answers

No need for regexp:

url.split("?")[0];

If you really need it, then try this:

\/path\?*.*

EDIT Actually the most precise regexp should be:

^(\/path)(\/?\?{0}|\/?\?{1}.*)$

because you want to match either /path or /path/ or /path?something or /path/?something and nothing else. Note that ? means "at most one" while \? means a question mark.

BTW: What kind of routing library does not handle query strings?? I suggest using something else.

like image 152
freakish Avatar answered Oct 11 '22 12:10

freakish