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.
A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters.
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.
Yes, URL query string params are of type string. It's up to you to convert them to and from the type you need.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With