In this article I am posting a code snippet in C# and VB.Net to remove or strip HTML Anchor Tags (Hyperlinks) from a Text string using Regular Expressions.
HTML Markup
Below is the HTML Markup where I have TextBox to enter HTML content with HTML Anchor Tags or Hyperlinks, a Label to display the converted string without HTML Anchor Tags or Hyperlinks and a Button to trigger the conversion
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" TextMode = "MultiLine" Height = "150px" Width = "500px" Text = "<a href = 'http://www.google.com'>Google</a> is a Search Engine. <a href = 'http://www.asp.net'>ASP.Net</a> is a web technology"></asp:TextBox><br />
<asp:Button ID="btnStrip" runat="server" Text="Remove Hyperlinks"
onclick="btnStrip_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</form>
</body>
</html>
Namespaces
C#
using System.Text.RegularExpressions;
VB.Net
Imports System.Text.RegularExpressions
Stripping the HTML Anchor Tags (Hyperlinks)
Below on the Button Click event handler I have written code snippet that will strip the HTML Anchor Tags or Hyperlinks from the text and display it in the ASP.Net Label control
C#
protected void btnStrip_Click(object sender, EventArgs e)
{
Label1.Text = Regex.Replace(TextBox1.Text, "</?(a|A).*?>", "");
}
VB.Net
Protected Sub btnStrip_Click(sender As Object, e As EventArgs)
Label1.Text = Regex.Replace(TextBox1.Text, "</?(a|A).*?>", "")
End Sub
Demo
Downloads
Download the sample in C# and VB.Net using the download link provided below