1

I have a set of URLs from which I want to extract the value of a certain parameter.

For example:

http://example.com/#!/dp/dp.php?g=4346&h=fd34&kl=45fgh&bl=nmkh
http://example.com/#!/dp/dp.php?h=fd34&g=4346&kl=45fgh&bl=nmkh
http://example.com/#!/dp/dp.php?h=fd34&kl=45fgh&g=4346&bl=nmkh
http://example.com/#!/dp/dp.php?h=fd34&kl=45fgh&bl=nmkh&g=4346

I want use javascript (regex) to get the value of the parameter g.

Can someone help me with this?

TRiG
  • 10,148
  • 7
  • 57
  • 107
  • 1
    See http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript or http://stackoverflow.com/questions/4467664/get-url-parameters-from-current-url-using-prototype-javascript or http://stackoverflow.com/questions/439463/how-to-get-get-and-post-variables-with-jquery or http://stackoverflow.com/questions/979975/how-to-get-the-value-from-url-parameter. – Trott Aug 30 '11 at 13:06

6 Answers6

3
var gMatches = url.match(/[?&]g=[^&]*/);
var g = gMatches[1];

broken down:

[?&]         # starts with a ? or &
g=           # contains g and an equals
[^&]*        # contains any character (except &) 0+ times

very primitive but should work.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • 1
    why that leading ampersand? What if g was the leading argument? I'd replace it with [?&] – CaNNaDaRk Aug 30 '11 at 13:05
  • @CaNNaDaRk: Indeed, I'm half awake apparently. Fixed it, thanks for the heads up. – Brad Christie Aug 30 '11 at 13:08
  • Don't forget to mention that *String*.match returns an array, not a string. It will be a single-element array in this case; if you add the 'g' flag to the end of the regex you'll get multiple results in the array (and a good programmer ought to be prepared for that possibility). – Blazemonger Aug 30 '11 at 13:09
  • @CaNNaDaRk: Given it's a hash-bang, I doubt that check is needed. And I am emphasizing **primitive** in this example. – Brad Christie Aug 30 '11 at 13:12
2
var url = window.location.href;
getParameter("g=", url, "&");

function getParameter(parameter, buffer, delimiter)
{
   var start = buffer.indexOf(parameter);
   if (start == -1)
      return null;
   var data = buffer.slice(start);
   if(data.match(delimiter) != null)
      var end = data.indexOf(delimiter);
   else
      var end = data.length;
   data = data.slice(0, end);
   var eq = data.indexOf("=");
   data = data.slice(eq+1);
   return data;
}
MiamiZ
  • 73
  • 6
1

Use this function.

function getParameterByName(name) {

    var match = RegExp('[?&]' + name + '=([^&]*)')
                    .exec(window.location.search);

    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));

}

Call it like

var url = "http://domain.com/#!/dp/dp.php?g=4346&h=fd34&kl=45fgh&bl=nmkh";
var parm =  getParameterByName("g");

Disclaimer: Shameless port of James Padolsey code from http://james.padolsey.com/javascript/bujs-1-getparameterbyname/

naveen
  • 53,448
  • 46
  • 161
  • 251
1

try this:

var url = window.location.href;
var name = 'g';
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var results = new RegExp("[\\?&]" + name + "=([^&#]*)").exec(url);
if (results != null)
{
    var g_url_value = results[1];
}
Alex Pacurar
  • 5,801
  • 4
  • 26
  • 33
1
var str = 'http://domain.com/#!/dp/dp.php?g=4346&h=fd34&kl=45fgh&bl=nmkh';
var reg = /(\&|\?)g=([^&]+)&?/;
console.log(str.match(reg)[2]);
Molecular Man
  • 22,277
  • 3
  • 72
  • 89
1
var results = new Array;
results = url.match(/[\?\&]g=([^&]*)/);

This should contain only the first value of g, not the entire "g=blah" string.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180