0

I am trying to get "a=.*" from the argument list in a URL (http:/a.b.com/?a=123) in Javascript.

/(.*[&|?])(a=.+?)(&.*)/.exec ("http:/a.b.com/?a=123&b=123")

gives me the expected and desired :

["http:/a.b.com/?a=123&b=123", "http:/a.b.com/?", "a=123", "&b=123"]

However, there might not be a following "&arg=val" and I need to be able to search for an optional trailing "&.*" (so adding '?' after the '&' on the third group. Hence :

/(.*[&|?])(a=.+?)(&?.*)/.exec ("http:/a.b.com/?a=123&b=123")

which gives me the unexpected :

["http:/a.b.com/?a=123&b=123", "http:/a.b.com/?", "a=1", "23&b=123"]

The second group now has only 1 character after the '=' and the remainder two '23' are part of the third group..

What I might be doing wrong ? Also any advice to do this a different/better way is also welcomed.

Thanks very much in advance !

1 Answer 1

1

You have two possibilities:

  • [&?]a=(.+?)(?:&|$)
  • [&?]a=([^&]+)

So, either match non-greedily until you encounter a & or the end of the string, or match greedily all characters except &.

Also, I've shortened your regex and made the group only capture the value of the a parameter, without the a= part. The .* are not needed, your regex doesn't have to match the whole string unless it's anchored with ^ and/or $. And that | in the character class was also wrong.

Example with your test case:

alert(JSON.stringify(
  /[&?]a=([^&]+)/.exec("http:/a.b.com/?a=123&b=123")
))

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

5 Comments

I can understand the second option and used it and worked !. I am not familiar with ?: - so just going with the second option. (thanks for pointer on ?: - will check it out). Great stuff Lucas and this was a lightening fast response !
You're welcome. FYI, (?:...) is equivalent to (...) except it doesn't capture it's value. It's only used for simple grouping.
Oh, so one could also do [&?]a=(.+?)[&|$]+ ?
No, you have to use (&|$) because in a character class ([...]), regex metacharacters lose their special meaning. So, [&|$] means: one of the following characters: & or | or $ literally.
Used some other bits from your enhanced answer. I need to get the remainder of the URL hence the '.*'. I wrapped it with ^ and $ as per your recommendation. Alls working well. Thanks !

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.