-1

I want to find the value corresponding to a specific key1 in QUERY_STRING using regex.

example :

"page?key1=value1&key2=value2&key3=value3"

"page?key2=value2&key1=value1&key3=value3"

"page?key2=value2&key3=value3&key1=value1"

I've found a regex like the one below, but it doesn't work the way I want it to in this situation.

key1=([^&]*)

"page?kkkkkkkey1=value1&key2=value2&key3=value3"

"page?key2=value2&kkkkkkey1=value1&key3=value3"

"page?key2=value2&key3=value3&kkkkkkey1=value1"

1 Answers1

1

You could try

(\&|\?)key1=([^&]*)

This way the regex will match only if the the key1 is prepended by & or ?

Note: Now the value will be the second parenthesis match

Tolis Gerodimos
  • 3,782
  • 2
  • 7
  • 14