3

I'm struggling with creating regex to match URL path with query param that could be in any place.

For example URLs could be:

/page?foo=bar&target=1&test=1 <- should match
/page?target=1&test=2 <- should match
/page/nested?foo=bar&target=1&test=1 <- should NOT match
/page/nested?target=1&test=2 <- should NOT match
/another-page?foo=bar&target=1&test=1 <- should NOT match
/another-page?target=1&test=2 <- should NOT match

where I need to target param target specifically on /page

This regex works only to find the param \A?target=[^&]+&*.

Thanks!

UPDATE: It is needed for a third-party tool that will decide on which page to run an experiment. It only accepts setup on their dashboard with regular experssion so I cannot use code tools like URL parser.

1
  • \/page\?.*target=([^&]+) It will match in group 1 the content of target, in this case "1" Commented Aug 8, 2019 at 20:23

2 Answers 2

6

General rule is that if you want to parse params, use URL parser, not a custom regex.

In this case you can use for instance:

# http://a.b/ is just added to make URL parsing work
url = new URL("http://a.b/page?foo=bar&target=1&test=1")
url.searchParams.get("target")
# => 1
url.pathname
# => '/page'

And then check those values in ifs:

url = new URL("http://a.b/page?foo=bar&target=1&test=1")

url = new URL("http://a.b/page?foo=bar&target=1&test=1")
if (url.searchParams.get("foo") && url.pathname == '/page' {
  # ...
}

See also:

EDIT

If you have to use regex try this one:

\/page(?=\?).*[?&]target=[^&\s]*(&|$)

Demo

Explanation:

  • \/page(?=\?) - matches path (starts with / then page then lookahead for ?)
  • .*[?&]target=[^&\s]*($|&) matches param name target:
    • located anywhere (preceded by anything .*)
    • [?&] preceded with ? or &
    • followed by its value (=[^&\s]*)
    • ending with end of params ($) or another param (&)
Sign up to request clarification or add additional context in comments.

7 Comments

it’s needed for a tool that will decide on which page to run an experiment. It only accepts setup on their dashboard this way.
so I don’t have an option here
Updated regex will match /page?foo=bar?target=1&test=1 with 2 ?
What do you mean? Shouldn't it match?
@Kosmetika - you should edit your question and update it with the information in these comments. mrzasa's suggestion to use new URL is the most natural response, because your question doesn't mention the limitations that prevent you from doing it.
|
1

If you're looking for a regex then you may use:

/\/page\?(?:.*&)?target=[^&]*/i

RegEx Demo

RegEx Details:

  • \/page\?: Match text /page?:
  • (?:.*&)?: Match optional text of any length followed by &
  • target=[^&]*: Match text target= followed by 0 or more characters that are not &

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.