首页 > 代码库 > 正则表达式匹配a标签的href

正则表达式匹配a标签的href

JS代码:

<html><head><script language="javascript">     var a=<P><A href="http://www.mamicode.com/~abc/ccg/ab.jpg" width="3">文字</A><A width="4" style="color:#ddd; font-weight:bold;" mm_href="http://www.mamicode.com/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="http://www.mamicode.com/www.baidu.com" cbas <span>cchref</span><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=http://www.mamicode.com/"([^<>"]*)\"/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.$2);    } </script></head><body></body></html>
View Code

C#代码:

string html = "<P><A href=http://www.mamicode.com/‘~abc/ccg/ab.jpg/‘ width=‘3‘ height=/"4\">文字</A><A width=\"4\" style=\"color:#ddd; font-weight:bold;\" mm_href=http://www.mamicode.com/"http:www.baidu.com\"  href=http://www.mamicode.com/"http://bbs.cn.yimg.com/user_img/200701/31/soso1.jpg\" mce_href=http://www.mamicode.com/"http://bbs.cn.yimg.com/user_img/200701/31/jisuanji986_117025184198149.jpg\">cc</A> href=http://www.mamicode.com/"www.baidu.com\" cbas <span>cchref</span>  1<a df href=http://www.mamicode.com/"cc\"   <A width=\"5\" href=http://www.mamicode.com/"http://bbs.cn.yimg.com/user_img/200701/31/soso2.jpg\" mce_href=http://www.mamicode.com/"http://bbs.cn.yimg.com/user_img/200701/31/cc.jpg\"></A></P>";Regex reg = new Regex("<a([\\s]+|[\\s]+[^<>]+[\\s]+)href=http://www.mamicode.com/[/"|‘](?<href>[^<>\"‘]*)[\"|‘][^<>]*>", RegexOptions.IgnoreCase);MatchCollection matchCollection = reg.Matches(html);MessageBox.Show(matchCollection.Count.ToString());foreach (Match match in matchCollection){    MessageBox.Show(match.Groups["href"].ToString());}
View Code

 

正则表达式匹配a标签的href