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

2 Answers 2

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>

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

4 Comments

This is not working when I change the url string to location.search. Can't figure out why...
Please re-try now, I updated the snippet and added your fixed function.
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.
I have just made the group in the 2nd regex non-capturing, so it is final now.
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

1 Comment

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.

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.