正则表达式匹配a标签或div标签
这里以a标签为例
a标签的href
var a='<P><A href=\'~abc/ccg/ab.jpg\' width="3">文字</A><A width="4" style="color:#ddd; font-weight:bold;" mm_href="http:www.baidu.com" href="http://bbs.cn.yimg.com/user_img/200701/31/soso1.jpg" mce_href="http://bbs.cn.yimg.com/user_img/200701/31/jisuanji986_117025184198149.jpg">cc</A> href="www.baidu.com" cbas <span>cchref</span> 1<a dd href="ccc" <A width="5" href="http://bbs.cn.yimg.com/user_img/200701/31/soso2.jpg" mce_href="http://bbs.cn.yimg.com/user_img/200701/31/cc.jpg"></A></P>';
var b=/<a([\s]+|[\s]+[^<>]+[\s]+)href=(\"([^<>"\']*)\"|\'([^<>"\']*)\')[^<>]*>/gi;
var s=a.toLowerCase().match(b);
alert(s.length);
for(var i= 0;i<s.length;i++)
{
var ss = s[i].toLowerCase().match(b);
alert(RegExp.$3+RegExp.$4);
}
a标签的内容没有属性centent
var str = "Lorem ipsum <pre>text 1</pre> Lorem ipsum <pre>text 2</pre>";
str.replace(/<pre>(.*?)<\/pre>/g, function(match, g1) { console.log(g1); }); // text 1 text 2
a标签带属性的匹配内容
var str = `"<a class="sds">And more ...</a><a class="sds">And more ...dd</a>"`
var re = new RegExp("<\s*a[^>]*>(.*?)<\s*/\s*a>", "g");
var myArray = str.split(re);
console.log(myArray)
参考
Regex select all text between tags
9 Regular Expressions to strip HTML tags