19

Possible Duplicate:
How can I get query string values?

Trying to get a regex (Javascript).

Input:   url.html?id=14114&yolo=hahaha
Output:  id=14114
         yolo=hahaha

So this is what i have done so far: (\&|\?)(.*?)\=(.*?)

How do I also include the red circled regions?

enter image description here

2
  • 2
    Do you mean url.html?id=14114&yolo=hahaha? Commented Feb 4, 2013 at 0:55
  • Why does it need to be regex? Commented Feb 4, 2013 at 0:59

2 Answers 2

46

How about this pattern:

(\?|\&)([^=]+)\=([^&]+)
Sign up to request clarification or add additional context in comments.

6 Comments

I made a change after he posted. with his help: (\?|\&)([^=]+)\=([^\&]+)
I know this is a little late but this answer will not ignore url fragments (anything after the "#"). Try this one: /[(\?|\&)]([^=]+)\=([^&#]+)/g
As in the RFC: tools.ietf.org/html/rfc3986#section-3.3 The semicolon(';') character can also be used to delimit parameters, So I think regex pattern should be changed accordingly
@TMKasun is right, the expression should match "Matrix-Parameters" too! (?:\?|&|;)([^=]+)=([^&|;]+)
This pattern assumes param values are nonempty, for example it won't match a= in url.com?a=&b=2. To allow empty values, use ([^&]*) instead of ([^&]+).
|
22

Try using this:

[^&?]*?=[^&?]*

2 Comments

What about ; delimiter for query parameters?
Doesn't work when I put it in rubular.com

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.