1

How to get the URL array parameter from javascript i have an array for example http://localhost/search?&p[]=599 and i need to receive the 599 in javascript.I use the below function to get the parameter which is not array.How to get an array parameter. ?

function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}
Ramesh
  • 2,295
  • 5
  • 35
  • 64

2 Answers2

0

Some characters, like [, have special meaning in regular expressions, so you must escape them:

function getURLParameter(name) {
    return decodeURI(
        (
            RegExp(
              name.replace(/[.^$*+?()[{\\|]/g, '\\$&')
              + '=' + '(.+?)(&|$)'
            ).exec(location.search) || [,null]
        )[1]
    );
}

Also see What special characters must be escaped in regular expressions?

Community
  • 1
  • 1
Oriol
  • 274,082
  • 63
  • 437
  • 513
0

It isn't an array. It is just a name with [] in it.

It is almost as simple as passing it the name of the thing you want "p[]" but [ and ] have special meaning in a regular expression so you need to escape them.

Regex escape characters have special meaning in JavaScript string literals, so you need to escape them too.

getURLParameter("p\\[\\]"));
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335