日期:2014-05-16  浏览次数:20358 次

请教一个正则:匹配含有某个字符串的<div>标签
假如有一段文本是:
<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>

我要根据"id2",找到这个字符串所属的直系div,并将这个div里面的a标签的超链接文本通过分组取出来

最终取出来的希望是这样的分组:

$0 <div><span id="id2"><a href="#">链接b</a></span><div>temp2</div></div>
$1 链接b

(第一组返回该条件字符串所属的div内容,第二组返回该div里面的超链接文本)

------解决方案--------------------
/<div><span\s*id="id2"><a\s*href="[^"]*">(.*?)</a></span>\S*</div>/gi
------解决方案--------------------
JScript code

            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])

------解决方案--------------------
HTML code
<!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