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.

2
  • possible duplicate of How can I get query string values in JavaScript? Commented Jan 16, 2014 at 7: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... Commented Jan 16, 2014 at 7:21

2 Answers 2

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

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

Comments

0

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

url.match(/[?&]m=([^&]*)&p=([^&]*)/i);

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.