Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 从<;获取href;A>;html中的标记_Asp.net_Regex - Fatal编程技术网

Asp.net 从<;获取href;A>;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> 我想做的

嗨,我有一些信息显示在网页上的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

我的想法是:如果标签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
已排序。

说明 此表达式将: