0

Possible Duplicates:
Get query string values in JavaScript
Use the get paramater of the url in javascript

I have a long list of URLs where in one part of the URL I've got a command such as 'KEY=123'. I would like to find all those keys.

For Example: /somecommand?ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1

How could this be accomplished? My idea was just to search all the 'KEY' words and look for the number next to it - but I guess there is something much quicker for this.

The language of preference would be Javascript.

EDIT:

The URLs are cluttered and can't be extrapolated out of the text easily. a small example of the text:

  1. 2011-07-29 01:17:55.965/somecommand?ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1 200 685ms 157cpu_ms 87api_cpu_ms 0kb ABCABC/2.0 CFNetwork/485.12.7 Darwin/10.4.0 Paros/3.2.13`
  2. 2011-07-29 01:05:19.566 /somecommand?ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1 200 29ms 23cpu_ms 0kb ABCABC/2.0 CFNetwork/485.12.7 Darwin/10.4.0 Paros/3.2.13
  3. 2011-07-29 01:04:41.231 /somecommand?ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1 200 972ms 78cpu_ms 8api_cpu_ms 0kb ABCABC/2.0 CFNetwork/485.12.7 Darwin/10.4.0 Paros/3.2.13
Community
  • 1
  • 1
Roman
  • 4,443
  • 14
  • 56
  • 81
  • This is no a duplicate - my problem is that I've got a list of URLs where they are a little cluttered - i.e - has some stuff around the URLs, so I can't get out only the URL part and must run on the whole text. – Roman Jul 31 '11 at 15:03

3 Answers3

1

The Javascript you'd need would be something like -

var text = 'ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1&key=678';
var matches = text.match(/KEY=\d*|key=\d*/g);
for (i=0; i<matches.length; i++) {
   alert(matches[i]);
}

If you wanted just the number, you could do something like -

var text = 'ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1&key=678';
var matches = text.match(/KEY=\d*|key=\d*/g);
for (i=0; i<matches.length; i++) {
   alert(matches[i].toLowerCase().replace('key=',''));
}
ipr101
  • 24,096
  • 8
  • 59
  • 61
0

If you are interested only in the KEY value:

var regex = new RegExp("KEY=(\d+)");
var result = regex.exec(window.location.href);

result would be "123" in your case. If you have multiple lines, then:

var regex = new RegExp("KEY=(\d+)", "gm");
var results = regex.exec(window.location.href);

in this case results is an array.

sergio
  • 68,819
  • 11
  • 102
  • 123
0
a = "/somecommand?ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1";
a.match(/KEY\=(\d+)/gi)
Dumitru Ceban
  • 613
  • 4
  • 6