View Full Version : .match() returning blank?

09-08-2011, 11:18 PM
hey guys i have the following test html that i want to grab information out of in paticular .match(/\s(\d{1,2}\%)<\/td>/)[1]; now this works for the first % which returns 4%

on this line <td style="color: white;">&nbsp;(4%)</td>

but then i have the same matches again

CL = temp.match(/\s(\d{1,2}\%)<\/td>/)[1];
alert("CL = " + CL); //returns 4%

Neo = temp.match(/\s(\d{1,2}\%)<\/td>/)[2];
alert("Neo = " + Neo);//returns undefined and should return 2%

Oak = temp.match(/\s(\d{1,2}\%)<\/td>/)[3];
alert("Oak = " + Oak); //returns undefined and should return 14%

Queen = temp.match(/\s(\d{1,2}\%)<\/td>/)[4];
alert("Queen = " + Queen); // returns undefined and should return 27%

here is a link to the temp variable because it would get messy here

09-09-2011, 11:37 AM
No, that's not the right way of doing it. /...(...).../ generally will only give you one match for the one group you have inside the regex. If you need to match more than once, you'll need to specify the flag /.../g for "globally" and use exec() instead of match()

var myReg = /\s(\d{1,2}\%)<\/td>/g;
var match;
while(match=myReg.exec(temp)) {

rnd me
09-09-2011, 08:52 PM
var nums=temp.match(/&nbsp;(\d{1,2}%)<\/td>/mig).map(function(a){
return a.split(";")[1].split("%")[0] * 1;

alert(nums);//shows: 4,2,14,27

or, using .split() and non-capturing parens:

alert(nums);//shows: 4%,2%,14%,27%