2

I am trying to write a function to return a parameter value for a given url parameter.

I call the function with a parameter name as string and want to have a number (-string) returned.

This is what i came up with:

function getParam(param) {
    param = param
                .replace(/\[/g, "\\[")
                .replace(/\]/g, "\\]");
    var paramReg = new RegExp(param + "=([0-9]+)", "g");
    var result = paramReg.exec(location.search);

    console.log(param + ": " + result);

    return result;
}

Right now i am using this function on two scenarios, param can be either "tid" or "field_date_value[value][year]". It appears to work fine on "tid" but i have problems with how it works on "field_date_value[value][year]" and I do not get why.

The console gives me the following on

"?field_date_value_op=%3D&field_date_value[value][year]=2015&tid=8"

tid: tid=8,8
field_date_value\[value\]\[year\]: null

But it returns the correct values if i switch parameter positions like

"?field_date_value_op=%3D&tid=8&field_date_value[value][year]=2015"

tid: tid=8,8
field_date_value\[value\]\[year\]: field_date_value[value][year]=2015,2015

Anyone can tell what I am missing?

Thanks, Tom

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
Tom
  • 188
  • 8

2 Answers2

2

Here is the fixed version. You need to use "\\\[" in the [ and ] escaping regexes.

function getParam(param) {
    param = param.replace(/(?:\[|%5B)/g, "\\\[").replace(/(?:\]|%5D)/g, "\\\]");
    var paramReg = new RegExp("[\\?&]" + param + "=([0-9]+)", "g");
    var result = paramReg.exec(location.search);
    console.log(param + ": " + result);
    return result;
}

And here is a working sample:

function getParam(param) {
    param = param.replace(/(?:\[|%5B)/g, "\\\[").replace(/(?:\]|%5D)/g, "\\\]");
    var paramReg = new RegExp("[\\?&]" + param + "=([0-9]+)", "g");
    var result = paramReg.exec("http://google.com?field_date_value_op=%3D&field_date_value[value][year]=2015&tid=8");
    console.log(param + ": " + result);
    return result;
}

document.getElementById("res").innerHTML = getParam('field_date_value%5Bvalue%5D[year]');
<body>
<div id="res"/>
  </body>
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • This is not working when I change the url string to location.search. Can't figure out why... – Tom Apr 03 '15 at 07:52
  • Please re-try now, I updated the snippet and added your fixed function. – Wiktor Stribiżew Apr 03 '15 at 08:02
  • Actually I just found out these "[" and "]" in "location.search" really are "%5B" and "%5D". So the problem never has been the regex itself but the string I am searching on. Thanks for bringing me to that point. should have checked this in the first place. – Tom Apr 03 '15 at 08:06
  • 1
    I have just made the group in the 2nd regex non-capturing, so it is final now. – Wiktor Stribiżew Apr 03 '15 at 09:28
0

If you are using native js take a look here How can I get query string values in JavaScript? or here How to get the value from the GET parameters? if you are using jQuery

Community
  • 1
  • 1
dimonser
  • 675
  • 6
  • 12
  • First link is pretty much what i am doing right now and is not working either. Second link may be a solution for what I want to achieve but is way more complicated and I still don't get why my regex is not working properly on this one. – Tom Apr 03 '15 at 07:21