废话不多说,都在代码中,直接上
JS代码:
复制代码 代码如下:
<html>
<head>
<script language="javascript">
var a='<P><A href=https://www.jb51.net/article/\'~abc/ccg/ab.jpg\'>文字</A><A mm_href="http:" href="" mce_href="">cc</A> href="" cbas <span>cchref</span> 1<a dd href="https://www.jb51.net/article/ccc" <A href="" mce_href=""></A></P>';
var b=https://www.jb51.net/<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);
}
</script>
</head>
<body>
</body>
</html>
C#代码:
复制代码 代码如下:
string html = "<P><A href='https://www.jb51.net/~abc/ccg/ab.jpg' height=https://www.jb51.net/article/\"4\" >文字</A><A width=https://www.jb51.net/article/\"4\" style=https://www.jb51.net/article/\"color:#ddd; font-weight:bold;\" mm_href=https://www.jb51.net/article/\"http:\" href=https://www.jb51.net/article/\"\" mce_href=https://www.jb51.net/article/\"\">cc</A> href=https://www.jb51.net/article/\"\" cbas <span>cchref</span> 1<a df href=https://www.jb51.net/article/\"cc\" <A width=https://www.jb51.net/article/\"5\" href=https://www.jb51.net/article/\"\" mce_href=https://www.jb51.net/article/\"\"></A></P>";
Regex reg = new Regex("<a([\\s]+|[\\s]+[^<>]+[\\s]+)href=(\"(?<href>[^<>\"']*)\"|'(?<href>[^<>\"']*)')[^<>]*>", RegexOptions.IgnoreCase);
MatchCollection matchCollection = reg.Matches(html);
MessageBox.Show(matchCollection.Count.ToString());
foreach (Match match in matchCollection)
{
MessageBox.Show(match.Groups["href"].ToString());
}
小伙伴们是否了解了js及C#中使用正则表达式的异同点呢?有疑问的请留言,大家共同讨论。
您可能感兴趣的文章: