2

I have the following types of query string. It could be anyone of the below.

index.html?cr=33&m=prod&p=ded

index.html?cr=33&m=prod&p=ded&c=ddl&h=33&mj=ori

From the above query string, i wanted to extract only m & p "m=prod&p=ded". Otherway to do is split and get, that i have already done it. But I wanted to achieve it using regular expression.

Any help is highly appreciated.

Community
  • 1
  • 1
  • possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Barmar Jan 16 '14 at 07:09
  • Why use regular expressions for this? Seems overkill for such a simple task. Especially given that you can't reuse the code anywhere else... – CodingIntrigue Jan 16 '14 at 07:21

2 Answers2

2

You can use something like this :

(?<=&)m=[^& ]*\&p=[^& ]*

In javascript , It doesn't support ?<= positive Lookbehind.

So you can use the following code ( in javascript ):

var s="index.html?cr=33&m=prod&p=ded"
s.match(/m=[^& ]*\&p=[^& ]*/g)

Refer Regexp101

Community
  • 1
  • 1
Sujith PS
  • 4,776
  • 3
  • 34
  • 61
0

If both query parameters come one after another then you you can use:

url.match(/[?&]m=([^&]*)&p=([^&]*)/i);
anubhava
  • 761,203
  • 64
  • 569
  • 643