1

I have a URL as follows: www.mysite.com?paramNamePrefixXXX=value

What is the best way to parse the url for the parameter name / value where XXX is dynamic/unknown..

Since I don't know the parameter name at render time.. I'd like to match on the 'startswith.. 'paramNamePrefix' + XXX (where XXX is some string..) and return the value

jquery offer a simple way to do this?

patrick
  • 451
  • 2
  • 6
  • 16
  • 1
    http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript - use that as a base, then do string matching on the results. – WWW Dec 13 '11 at 16:46
  • Is XXX 3 characters? 3 Numbers? Will there be other parameters passed? – paulslater19 Dec 13 '11 at 16:47
  • XXX really could be anything.. So any combination of characters or numbers following 'key' – patrick Dec 13 '11 at 17:01

2 Answers2

0

This will parse it I believe. The only issue you would run into with the way it's written is if there was an = sign in your parameter value somehow.

((?<=&|\?).+?)(?<=\=)(.+?(?=&|$))

basically I've got it in 2 reference groups

((?<=&|\?).+?) <-- captures parameter name using a look behind

(?<=\=)

(.+?(?=&|$)) <-- captures parameter value using a look ahead

Doug Chamberlain
  • 11,192
  • 9
  • 51
  • 91
  • thanks Doug.. I updated my question to be more specific.. The paramater name is what will change.. It will also contains some prefix.. so 'paramNamePrefix' + someString.. I'd like to create something that can fetch 'paramNamePrefix + someString' and it's value.. Sorry for the confusion – patrick Dec 13 '11 at 17:23
  • Yes, that is what this regex will capture. This is even going to work if the paramNamePrefix changes. – Doug Chamberlain Dec 13 '11 at 18:47
0
var url = "http://www.mysite.com?foo=bar&paramNamePrefixXXX=value&fizz=buzz",
    prefix = "paramNamePrefix";

var desiredValue = url.match(new RegExp('[?&]' + prefix + '.*?=(.*?)[&#$]', ''));
desiredValue = desiredValue && desiredValue[1];

console.log(desiredValue); // -> "value"

Demo

mVChr
  • 49,587
  • 11
  • 107
  • 104