In this article I will explain with an example, how to remove or strip HTML Anchor Tags (HyperLinks) from a Text string using
Regular Expression in ASP.Net using C# and VB.Net.
HTML Markup
The following HTML Markup consists of:
TextBox – For capturing user input.
The TextBox has been assigned with followin Property:
TextMode – For displaying multiple text input box, here it is multiple where users can input multiple lines of text.
Button – For removing hyperlinks.
The Button has been assigned with an OnClick event handler.
Label – For displaying message.
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Height="100px" 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 /><br />
<asp:Button ID="btnStrip" runat="server" Text="Remove Hyperlinks" OnClick="OnStrip" /><br /><br />
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
Namespaces
You will need to import the following namespace.
C#
using System.Text.RegularExpressions;
VB.Net
Imports System.Text.RegularExpressions
Stripping the HTML Anchor Tags (Hyperlinks)
When the Remove Hyperlinks button is clicked, the HyperLink will be replace with the text using Replace method of Regex and then the message is set in a Label control.
C#
protected void OnStrip(object sender, EventArgs e)
{
lblMessage.Text = Regex.Replace(txtBody.Text, "</?(a|A).*?>", "");
}
VB.Net
Protected Sub OnStrip(sender As Object, e As EventArgs)
lblMessage.Text = Regex.Replace(txtBody.Text, "</?(a|A).*?>", "")
End Sub
Screenshot
Demo
Downloads