例如有如下串:
复制代码 代码如下:
<li><a href="http://www.abcxyz.com/something/article/143.htm" title="FCKEditor高亮代码插件测试"><span ="article-date">[09/11]</span>FCKEditor高亮代码插件测试</a></li>
现在需要提取 href 后面网址内日期和 链接文字
下面给出C# ASP 和 Javascript 实现方式
C#实现
复制代码 代码如下:
strHTML = "<li><a \"href=http://www.abcxyz.com/something/article/143.htm\" title=\"FCKEditor高亮代码插件测试\"><span =\"article-date\">[09/11]</span>FCKEditor高亮代码插件测试</a></li>";
pattern = "http://([^\\s]+)\".+?span.+?\\[(.+?)\\].+?>(.+?)<";
Regex reg = Regex( pattern, RegexOptions.IgnoreCase );
MatchCollection mc = reg.Matches( strHTML );
(mc.Count > 0)
{
foreach (Match m in mc)
{
Console.WriteLine( m.Groups[1].Value );
Console.WriteLine( m.Groups[2].Value );
Console.WriteLine( m.Groups[3].Value );
}
}
ASP实现
复制代码 代码如下:
<%
Dim str, reg, objMatches
str = "<li><a href=""http://localhost/Z-Blog18/article/143.htm"" title=""FCKEditor高亮代码插件测试""><span =""article-date"">[09/11]</span>FCKEditor高亮代码插件测试</a></li>"
Set reg = RegExp
reg.IgnoreCase = True
reg.Global = True
reg.Pattern = "http://([^\s]+)"".+?span.+?\[(.+?)\].+?>(.+?)<"
Set objMatches = reg.Execute(str)
If objMatches.Count > 0 Then
Response.Write("网址:")
Response.Write(objMatches(0).SubMatches(0))
Response.Write("<br>")
Response.Write("日期:")
Response.Write(objMatches(0).SubMatches(1))
Response.Write("<br>")
Response.Write("标题:")
Response.Write(objMatches(0).SubMatches(2))
End If
%>
Javascript实现
复制代码 代码如下:
<script type="text/javascript">
var str = '<li><a href="http://localhost/Z-Blog18/article/143.htm" title="FCKEditor高亮代码插件测试"><span ="article-date">[09/11]</span>FCKEditor高亮代码插件测试</a></li>';
var pattern = /http:\/\/([^\s]+)".+?span.+?\[(.+?)\].+?>(.+?)</gi;
var mts = pattern.exec(str);
(mts != null)
{
alert(mts[1]);
alert(mts[2]);
alert(mts[3]);
alert(mts[4]);
}
</script>
最新评论