PDA

View Full Version : Advanced regex question



extnewbie
18 Aug 2008, 7:18 AM
Hi,

I have a long json string that has included the string: "size":"100,100" several times.
where the numbers 100,100 is different each time.
I can find the first occurance of the string with the regex below.
But with more occurances: xy[1],xy[2] it dosen't work

var xy = rest.match(/"size":\"\s?([0-9]+)\s?,\s?([0-9]+)\s?\"/);
alert(xy[0]);

Can anybody tell whats wrong?

Thanks

devnull
18 Aug 2008, 7:41 AM
you need to provide the "g" or global flag:


var xy = rest.match(/"size":\"\s?([0-9]+)\s?,\s?([0-9]+)\s?\"/g);
//xy is now an array of arrays
alert(xy[0][0]);

heres a good simple overview of regex's in javascript: http://www.evolt.org/article/Regular_Expressions_in_JavaScript/17/36435/

extnewbie
18 Aug 2008, 8:05 AM
Thanks!