[英]get the href from an <A> tag in html
嗨,我有一些信息正在网页上显示HTML内容。
其中一些内容可以包含:
<p>
<a href="/documents/content/files/my_mp3.mp3" target="_blank">
<img alt="" src="/pictures/content/images/play.jpg" style="width: 153px; height: 49px;"><br>
</a>
</p>
我想要做的是获取整个标签,甚至只是“ href”,并将其存储在变量中...这样我就可以将其显示在其他地方,(即以摘要形式显示)
图像始终是一致的,因此我真正需要的只是href。
我在想类似的东西:如果<a>标签href包含“ .mp3”,则获取“ href”
<a>标记不包含ID或符号。 它是使用javascript HTML编辑器输入的,因此没有id或runat。
有人知道该怎么做吗?
谢谢
该表达式将:
将避免困难的html边缘情况
<a\\b(?=\\s)(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\\s>]*)*?\\shref="([^"]*.mp3)")(?:[^>=]|='[^']*'|="[^"]*"|=[^'"\\s]*)*\\s?>.*?<\\/a>
实时示例: http : //www.rubular.com/r/CMmFQDq0qX
示范文本
注意第二个anchor标签有一些非常困难的边缘情况,这会使大多数表达式跳闸
<p>
<a href="/documents/content/files/my_mp3.mp3" target="_blank">
<img alt="" src="/pictures/content/images/play.jpg" style="width: 153px; height: 49px;"><br>
</a>
<a onmouseover=' href="NotTheMP3Yourelookingfor.mp3" ; if (6 > x) { funRotate(href) ; } ; ' href="/documents/content/files/DifficultToFind.mp3" target="_blank">
<img alt="" src="/pictures/content/images/play.jpg" style="width: 153px; height: 49px;"><br>
</a>
</p>
火柴
组0将具有整个<a..>..</a>
标记
第1组将仅具有href值
[0][0] = <a href="/documents/content/files/my_mp3.mp3" target="_blank">
<img alt="" src="/pictures/content/images/play.jpg" style="width: 153px; height: 49px;"><br>
</a>
[0][1] = /documents/content/files/my_mp3.mp3
[1][0] = <a onmouseover=' href="NotTheMP3Yourelookingfor.mp3" ; if (6 > x) { funRotate(href) ; } ; ' href="/documents/content/files/DifficultToFind.mp3" target="_blank">
<img alt="" src="/pictures/content/images/play.jpg" style="width: 153px; height: 49px;"><br>
</a>
[1][1] = /documents/content/files/DifficultToFind.mp3
因此,假设您使用的标记是您正在使用ASP.NET。
<a href="/documents/content/files/my_mp3.mp3" target="_blank"
id="myAnchor" runat="server">
'runat'允许您在'id'后面的代码中引用它:
string href = myAnchor.HRef;
现在,字符串“ href” =“ /documents/content/files/my_mp3.mp3”,那么您可以根据需要将其拆分。
如果要在服务器端使用它,则有两个选择:
A.添加runat和id属性:
`<a href="/documents/content/files/my_mp3.mp3" id="myLink" runat="server" target="_blank">`
然后,您可以通过调用myLink.HRef
来访问href。
B.您可以改用asp:Hyperlink服务器控件
`<asp:Hyperlink NavigateUrl="yourhref.html" id="myLink" runat="server" />`
它将也呈现为锚,您可以通过调用myLink.NavigateUrl
来访问href。
编辑:
如果链接是在客户端生成的,那么您必须进行一些修改,因为服务器代码不会直接看到此链接。
所以首先是添加一个隐藏的输入控件。 此控件将临时存储您的href,以便您回发时可以访问它。
<input type="hidden" runat="server" id="tempContainer" />
那么您需要在创建超链接之后(或在回发之前)运行一些JavaScript。 在此示例中,我使用的是jquery,但您也可以使用普通的javascript。
$('.<%=tempContainer.ClientID %>').val($('.some selector to your link').attr('href'));
这会将您的href值保存到隐藏的输入中。 因此,当您发回邮件时,可以通过调用tempContainer.Value
来访问它的值。
好吧,我想我已经做到了:
If strContent.Contains(".mp3""") Then 'check if content contains .mp3
'find the href tag usin regex
Dim pattern As String = "(href=\s*\""(.+?)\"")"
Dim html As String = strContent
Dim href() As String = Regex.Split(html, pattern)
Dim href1 As String = href(1) 'gets the href tag
'make sure that this href contains contains .mp3 (and it's not some other)
If href1.Contains(".mp3""") Then
'this is a hidden control to display on summary main category page
Dim atemp As hyperlink = e.Item.FindControl("lnkSound")
'remove the href= and quotes from the href
'set the naviate url
atemp.navigateurl = href1.Replace("href=", "").Replace(chr(34), "")
atemp.target = "_blank"
'make it visible as hidden for summarys that don't contain .mp3
atemp.visible = True
End If
End If
排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.