1

I need to match a set of URLs and they all must contain a specific query param

(type=VCL)

So far I have /http:\/\/myhost:8080\/organization\/(\d*)\/mediaResource?.*$/

Which matches urls formed like http://myhost:8080/organizaton/5/mediaResource?anyParam(s) but I need to only get matches if the the URL contains the query param type=VCL

like: http://myhost:8080/organization/5/mediaResource?someParam=foo&type=VCL

Can someone help with the last part of the regex to get only URLs that contain the query param I care about?

2
  • check this:regex101.com/r/lO1eS5/1 this very simpe the expression need to be type=VCL and that it's unless i didn't understand what you looking for... Commented Jul 8, 2015 at 22:12
  • i would use a queryString parser instead of a regexp, which is fragile when considering all the forms of entity encoding that are legit. Commented Jul 8, 2015 at 22:23

2 Answers 2

2

You can use the following regex:

http:\/\/myhost:8080\/organization\/(\d*)\/mediaResource\?\S*\btype=VCL\b\S*

See demo

I only added a slash before the ? so that it was matched literally, added \S* matching 0 or more non-whitespace symbols, and \btype=VCL\b so that only those URLs that have this param are matched.

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

Comments

2

I'd strongly recommend using a URI parser for this - it's easier to read and more maintainable and someone has done the work of testing the constituent regexes.

I've had good success with this lightweight one:

http://blog.stevenlevithan.com/archives/parseuri

2 Comments

This is for some inline routing in some mocking for Protractor, do you think your suggestion would work for that?
I don't see why not. If anything, having a URI parsed into an easily manipulable structure by long-tested code should make this easier. For instance it's trivial to change your logic to use some other query param, or an anchor tag or the host or some combination. Whereas you might have a hard time remembering exactly how your custom, single param regex works, let alone reliably modifying it, a month from 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.