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 with C# and VB.Net.
HTML Markup
The HTML Markup consists of following controls:
TextBox – For capturing user input.
The TextBox has been assigned with TextMode property set to Multiline to allow 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"></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 replaced with the empty string using
Replace method of
Regular Expression and then the message is set into 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
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Demo
Downloads