日期:2014-05-16 浏览次数:20358 次
var str= '<div><span id="id1"><a href="#">链接a</a></span><div>temp1</div></div><div><span id="id2"><a href="#">链接b</a></span><div>temp2</div></div><div><span id="id3"><a href="#">链接c</a></span><div>temp3</div></div>'; var re = /<div>.+?<a.+?>(.+?)<\/a>.+?<\/div>/gi; var tmp = []; var a = str.match(re); for(var i = 0, len = a.length; i < len; i++){ tmp[i] = []; tmp[i].push(a[i]); a[i].match(re); tmp[i].push(RegExp.$1); } alert(tmp.length) alert(tmp[0][0]) alert(tmp[0][1]) alert(tmp[1][0]) alert(tmp[1][1]) alert(tmp[2][0]) alert(tmp[2][1])
------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <div id="demo" style="display:none;"></div> <script type="text/javascript"> var html = '<div><span id="id1"><a href="#">链接a</a></span><div>temp1</div></div>\ <div><span id="id2"><a href="#">链接b</a></span><div>temp2</div></div>\ <div><span id="id3"><a href="#">链接c</a></span><div>temp3</div></div>'; function f(html, id) { document.getElementById('demo').innerHTML = html; var result = []; var obj = document.getElementById(id); if (obj.length < 1) result.push('Not found!'); else { result.push('<div>' + obj.parentNode.innerHTML + '</div>'); result.push(obj.getElementsByTagName('a')[0].innerHTML); } return result; } alert(f(html, 'id2')); </script> </body> </html>
------解决方案--------------------
/<div><span\s*id="id2"><a\s*href="[^"]*">(.*?)</a></span>[\d\D]*?</div></div>/gi