Asp.net 从<;获取href;A>;html中的标记
嗨,我有一些信息显示在网页上的HTML内容 某些内容可能包含:Asp.net 从<;获取href;A>;html中的标记,asp.net,regex,Asp.net,Regex,嗨,我有一些信息显示在网页上的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> 我想做的
<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
我的想法是:如果标签href包含“.mp3”,则获取“href”
标记不包含ID或runat。它是使用javascript html编辑器输入的,因此没有id或runat。 有人知道怎么做吗
谢谢根据您的标签,我假设您正在使用ASP.NET,因此
<a href="/documents/content/files/my_mp3.mp3" target="_blank"
id="myAnchor" runat="server">
字符串“href”现在是=“/documents/content/files/my_mp3.mp3”,然后您可以随意将其拆分。根据您的标记,我假设您正在使用ASP.NET
<a href="/documents/content/files/my_mp3.mp3" target="_blank"
id="myAnchor" runat="server">
字符串“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.NavigateUrl
编辑: 如果链接是在客户端生成的,那么您必须进行一些修补,因为服务器代码无法直接看到该链接 所以首先是添加一个隐藏的输入控件。此控件将临时存储您的href,以便您在发回时可以访问它
然后,您需要在创建超链接后(或发回之前)运行一些javascript。在本例中,我使用jquery,但您也可以使用普通javascript
$('.<%=tempContainer.ClientID %>').val($('.some selector to your link').attr('href'));
$('..).val($('.链接的某些选择器').attr('href');
这会将href值保存到隐藏的输入中。因此,当您发回时,您可以通过调用来访问它的值:
tempContainer.value
如果您想在服务器端使用它,那么您有两个选项:
A.添加runat和id属性:
`<a href="/documents/content/files/my_mp3.mp3" id="myLink" runat="server" target="_blank">`
这也将作为锚呈现,您可以通过调用myLink.NavigateUrl
编辑: 如果链接是在客户端生成的,那么您必须进行一些修补,因为服务器代码无法直接看到该链接 所以首先是添加一个隐藏的输入控件。此控件将临时存储您的href,以便您在发回时可以访问它
然后,您需要在创建超链接后(或发回之前)运行一些javascript。在本例中,我使用jquery,但您也可以使用普通javascript
$('.<%=tempContainer.ClientID %>').val($('.some selector to your link').attr('href'));
$('..).val($('.链接的某些选择器').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
排序。好的,我想我已经成功地得到了:
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
已排序。说明
此表达式将:
- 匹配以.mp3结尾的href属性的锚定标记
- 将避免困难的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> <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将具有整个
说明 此表达式将:[0][1]=/documents/content/files/my_mp3.mp3 [1][0] = [1] [1]=/documents/content/files/difficultofind.mp3
- 匹配以.mp3结尾的href属性的锚定标记
- 将避免困难的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> <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将具有整个
您的HTML是如何生成的?另外,你能举例说明你是如何使用你提取的信息的吗?你可以编写一些[0][1]=/documents/content/files/my_mp3.mp3 [1][0] = [1] [1]=/documents/content/files/difficultofind.mp3
代码来存储变量value@GHC用户正在将整个产品的html输入CMS。@mike27015我正在使用asp.NET。您想在哪里使用此信息?在客户端中(即javascript)?或者在服务器端(asp.net)?HTML是如何生成的?另外,你能举例说明你是如何使用你提取的信息的吗?你可以编写一些php
代码来存储变量value@GHC用户正在将整个产品的html输入CMS。@mike27015我正在使用asp.NET。您想在哪里使用此信息?在客户端中(即javascript)?或者在服务器端(asp.net)?不幸的是,a href是在ckeditor中生成的,因此没有标记的id…您想用它做什么?没有办法转换到VisualStudio之类的软件吗?id/runat机制对于ASP.NET来说是相当核心的,而不是真正的Sam。标记不包含id或runat。它是在html编辑器中使用javascript输入的,因此没有id或runat。如果您只是从自动生成的页面获取数据,那么我可能会说您最好的选择就是将页面作为纯文本,并使用字符串操纵手动搜索锚定标记。。如果您使用ckeditor创建页面,那么最好切换到允许您指定id和runat属性的东西MatchCollection对象和合适的正则表达式字符串应该可以做到这一点:不幸的是,a href是在ckeditor中生成的,因此,标记没有id…您想用它做什么?没有办法转换到VisualStudio之类的软件吗?这个php