开发者

Return a number of matches

I am trying to get a number of matches since it matches two. Right now I am just getting one from console.log().

<script type="text/javascript">
开发者_如何学运维
var page = 
'<tr class="Row">' +
    '<td class="1"><p>12/31/2010</p></td>'+
    '<td class="2">'+
                '<p>text</p>'+
    '</td>'+
'</tr>'+
'<tr class="Row">' +
    '<td class="1"><p>12/01/2009</p></td>'+
    '<td class="2">'+
                '<p>text</p>'+
    '</td>'+
'</tr>'
;

var pattern = /<td class="1"><p>((\d){2}(?=\/)\/(\d){2}(?=\/)\/20(\d){2})<\/p><\/td>/;
var match = page.match(pattern);  
console.log(page);
console.log(match);

</script>

Is there a way to retrieve all the matches.


just use the options.

pattern = /.../gm;


I tend to use .exec() for Regex in JavaScript with the g modifier at the end of the pattern. You can then iterate over the matches by calling this multiple times until match is null:

var pattern = /<td class="1"><p>((\d){2}(?=\/)\/(\d){2}(?=\/)\/20(\d){2})<\/p><\/td>/g;
var match = pattern.exec(page);  
console.log(page);

while(match){
   console.log(match);
   match = pattern.exec(page);
}

See this working: http://jsfiddle.net/jonathon/EtpQp/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜