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