Simple RegExp question - how to find keys [duplicate]
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:
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`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
2011-07-29 01:04:41.231 /somecommand?ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1 200 972ms 开发者_如何学Python78cpu_ms 8api_cpu_ms 0kb ABCABC/2.0 CFNetwork/485.12.7 Darwin/10.4.0 Paros/3.2.13
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=',''));
}
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.
a = "/somecommand?ACTION=UPDATE&DATATYPE=1&KEY=462&NUMBER=123.5263&SOMEID=845&IDTYPE=1";
a.match(/KEY\=(\d+)/gi)
精彩评论