200分解决一正则问题
"<div class="testfdsf">fdsf</div>fdssdf<div class="testfdsf">fdsf</div>fdsf<div class="testfdsf">fdsf</div>fdsf<div class="testfdsf">fdsf</div>"
如何将<div class="test.*"></div>这样的部分取出做为一个数组?
------解决方案--------------------你是取标签还是取标签里面的字段?
------解决方案-------------------- <script language="javascript" type="text/javascript">
var str = '<div class="testfdsf">fdsf</div>fdssdf<div class="testfdsf">fdsf</div>fdsf<div class="testfdsf">fdsf</div>fdsf<div class="testfdsf">fdsf</div>"';
var pattern = /<div\s*class=\"test.*?\"\s*>(.*?)<\/div>/g
var ary = str.match(pattern);
alert(ary);
</script>
------解决方案--------------------var c="<div class=\"testfdsf\">fdsf</div>fdssdf<div class=\"testfdsf\">fdsf</div>fdsf<div class=\"testfdsf\">fdsf</div>fdsf<div class=\"testfdsf\">fdsf</div>";
var reg=/<div class="test.*?">.*?<\/div>/gi;
c=c.match(reg);
for(var i in c){
alert(c[i]);
}
这样?不很明白你的意思
------解决方案--------------------
var s = '<div id="abc" class="testabc">testabc</div>fdssdf' +
'<div class="xyz">xyz</div>fdsf' +
'<div class="testfdsf">fdsf</div>fdsf' +
'<div class="abc">abc</div>',
reg = /<div\s+[^>]*?class=['"]test.+?<\/div>/gi;
alert(s.match(reg).join(',\r\n'))
------解决方案--------------------双引号后面加个问号。
/<div class=['"]?test[^>]*>.+?<\/div>/gi;
------解决方案--------------------感觉这种设计怪怪的 class为什么不设为 class="test param1 param2 ..."用空格分开形式
去找下 getElementsByClassName 的源码应该可以很容易修改成所需求的.