从html中的标签获取href

时间:2022-11-27 17:42:21

Hi I have some information being displayed HTML content on a web page.

嗨,我有一些信息在网页上显示HTML内容。

Some of the content can contain:

部分内容可包含:

<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>

What I'm looking to do is to take the whole tag or even just the "href" and store it in a variable...so I can display it somewhere else, (i.e. display it in a summary)

我要做的是取整个标签,甚至只是“href”并将其存储在变量中...所以我可以在其他地方显示它,(即在摘要中显示)

The image is always consistent so all I really need is the href.

图像总是一致的,所以我真正需要的是href。

I was thinking something like: if an < a > tag href contains ".mp3" then get the "href"

我想的是:如果标签href包含“.mp3”,那么得到“href”

The < a > tag does not contain an ID or runat. It's being entered using a javascript html editor, so there's no id or runat.

标记不包含ID或runat。它是使用javascript html编辑器输入的,因此没有id或runat。

Anyone know how this could be done?

任何人都知道如何做到这一点?

Thanks

谢谢

4 个解决方案

#1


2  

Description

This expression will:

这个表达式将:

从html中的标签获取href

Example

Live example: http://www.rubular.com/r/CMmFQDq0qX

实例:http://www.rubular.com/r/CMmFQDq0qX

Sample Text

示例文本

Note the second anchor tag has some pretty difficult edge cases which will trip up most expressions

请注意,第二个锚标记有一些非常困难的边缘情况,这会使大多数表达式绊倒

<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>

Matches

火柴

Group 0 will have the entire <a..>..</a> tag
Group 1 will have just the href value

组0将具有整个 .. 标记组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

#2


1  

I'm assuming by your tags you're using ASP.NET, therefore..

我假设你的标签你正在使用ASP.NET,因此..

<a href="/documents/content/files/my_mp3.mp3" target="_blank"
                                              id="myAnchor" runat="server">

The 'runat' lets you reference it in the code behind by the 'id':

'runat'允许您在'id'后面的代码中引用它:

string href = myAnchor.HRef;

The string 'href' is now = "/documents/content/files/my_mp3.mp3", then you can break it apart as you please.

字符串'href'现在=“/documents/content/files/my_mp3.mp3”,然后您可以随意分解它。

#3


0  

If you want to use it server side, then you have two options:

如果你想在服务器端使用它,那么你有两个选择:

A. add the runat and id attribute:

A.添加runat和id属性:

`<a href="/documents/content/files/my_mp3.mp3" id="myLink" runat="server" target="_blank">`

Then you can access the href by calling myLink.HRef

然后,您可以通过调用myLink.HRef来访问href

B. You can use an asp:Hyperlink server control instead

B.您可以使用asp:超链接服务器控件

`<asp:Hyperlink NavigateUrl="yourhref.html" id="myLink" runat="server" />`

This will be rendered as an anchor too and you can access the href by calling myLink.NavigateUrl

这也将呈现为锚点,您可以通过调用myLink.NavigateUrl来访问href


EDIT:

编辑:

if the link is being generated in client side, then you have to do some tinkering because this link won't be directly visible to the server code.

如果链接是在客户端生成的,那么你必须做一些修补,因为这个链接不会直接对服务器代码可见。

So first is add a hidden input control. This control will temporarily store your href so you can access it when you post back.

首先是添加一个隐藏的输入控件。此控件将临时存储您的href,以便您在回发时可以访问它。

<input type="hidden" runat="server" id="tempContainer" />

then you need to run some javascript after you create the hyperlink (or before posting back). I'm using jquery in this example, but you can also use plain javascript.

然后你需要在创建超链接后(或在回发之前)运行一些javascript。我在这个例子中使用jquery,但你也可以使用普通的javascript。

$('.<%=tempContainer.ClientID %>').val($('.some selector to your link').attr('href'));

This saves your href value into the hidden input. So when you post back, you can access the value of it by calling: tempContainer.Value

这会将您的href值保存到隐藏的输入中。因此,当您回发时,您可以通过调用:tempContainer.Value来访问它的值

#4


0  

Ok I think I've managed to get this:

好吧,我想我已经设法得到了这个:

        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

Sorted.

排序。

#1


2  

Description

This expression will:

这个表达式将:

从html中的标签获取href

Example

Live example: http://www.rubular.com/r/CMmFQDq0qX

实例:http://www.rubular.com/r/CMmFQDq0qX

Sample Text

示例文本

Note the second anchor tag has some pretty difficult edge cases which will trip up most expressions

请注意,第二个锚标记有一些非常困难的边缘情况,这会使大多数表达式绊倒

<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>

Matches

火柴

Group 0 will have the entire <a..>..</a> tag
Group 1 will have just the href value

组0将具有整个 .. 标记组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

#2


1  

I'm assuming by your tags you're using ASP.NET, therefore..

我假设你的标签你正在使用ASP.NET,因此..

<a href="/documents/content/files/my_mp3.mp3" target="_blank"
                                              id="myAnchor" runat="server">

The 'runat' lets you reference it in the code behind by the 'id':

'runat'允许您在'id'后面的代码中引用它:

string href = myAnchor.HRef;

The string 'href' is now = "/documents/content/files/my_mp3.mp3", then you can break it apart as you please.

字符串'href'现在=“/documents/content/files/my_mp3.mp3”,然后您可以随意分解它。

#3


0  

If you want to use it server side, then you have two options:

如果你想在服务器端使用它,那么你有两个选择:

A. add the runat and id attribute:

A.添加runat和id属性:

`<a href="/documents/content/files/my_mp3.mp3" id="myLink" runat="server" target="_blank">`

Then you can access the href by calling myLink.HRef

然后,您可以通过调用myLink.HRef来访问href

B. You can use an asp:Hyperlink server control instead

B.您可以使用asp:超链接服务器控件

`<asp:Hyperlink NavigateUrl="yourhref.html" id="myLink" runat="server" />`

This will be rendered as an anchor too and you can access the href by calling myLink.NavigateUrl

这也将呈现为锚点,您可以通过调用myLink.NavigateUrl来访问href


EDIT:

编辑:

if the link is being generated in client side, then you have to do some tinkering because this link won't be directly visible to the server code.

如果链接是在客户端生成的,那么你必须做一些修补,因为这个链接不会直接对服务器代码可见。

So first is add a hidden input control. This control will temporarily store your href so you can access it when you post back.

首先是添加一个隐藏的输入控件。此控件将临时存储您的href,以便您在回发时可以访问它。

<input type="hidden" runat="server" id="tempContainer" />

then you need to run some javascript after you create the hyperlink (or before posting back). I'm using jquery in this example, but you can also use plain javascript.

然后你需要在创建超链接后(或在回发之前)运行一些javascript。我在这个例子中使用jquery,但你也可以使用普通的javascript。

$('.<%=tempContainer.ClientID %>').val($('.some selector to your link').attr('href'));

This saves your href value into the hidden input. So when you post back, you can access the value of it by calling: tempContainer.Value

这会将您的href值保存到隐藏的输入中。因此,当您回发时,您可以通过调用:tempContainer.Value来访问它的值

#4


0  

Ok I think I've managed to get this:

好吧,我想我已经设法得到了这个:

        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

Sorted.

排序。