0

I looked at this question:

What is a good regular expression to match a URL?

And took the most up-voted answer which is this regex:

^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$

This works great to validate urls, HOWEVER, I also need this to allow strings such as this:

http://www.cool.com:81/index.html?query=${query}&sortBy=${sortBy}

Notice the placeholder strings such as ${query} and ${sortBy} (there can be any letters a-z inside the brackets). These are later replaced with actual values so the final url would look like this:

http://www.cool.com:81/index.html?query=helloworld&sortBy=population

I've been playing around with the regex to see if I can add a condition where those placeholders are allowed but none of my attempts have been successful unfortunately.

How can I do this?

EDIT:

The duplicate question provided does not answer how to match ${query} but rather specific urls that just has parameters.

9
  • 1
    Possible duplicate of Regex to match URL with specific query string param Commented Apr 19, 2018 at 9:27
  • ^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b((?:[-a-zA-Z0-9@:%_\+.~#?&//=\$\{\}])*)$ Commented Apr 19, 2018 at 9:34
  • @dfsq: This regex will not work when =${sortBy}&& or }}}}}} or $$$$$ Commented Apr 19, 2018 at 9:36
  • @TanDuong I know, but I'm not answerging question. Original regexp is not good to start with. Commented Apr 19, 2018 at 9:42
  • @IgnacioAra I don't see how this is a duplicate of that question. I'm asking a completely separate thing. Commented Apr 19, 2018 at 10:14

1 Answer 1

0
^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)\?query=([a-z]+)&sortBy=([a-z]+)$

In Group 3, you have your query, and in Group 4, your sortBy

See it here:

https://regex101.com/r/2pw8Yh/1

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

Comments

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.