Skip to main content

Regular Expression To Strip/Remove Html Tags From String in ASP.Net C#.Net VB.Net

In this tutorial i am going to explain about how to strip/remove html tags from the string using regular expression in asp.net and C#.Net or It is achieved by using the System.Text.RegularExpressions namespace.


In my previous article i have explained about Sort Dictionary Based On Value In Asp.Net , Get All Coutries From System.Globalization Namespace , Web Api Model Validation Using Validation Filters , Convert String To Upper,Lower & Title(Proper) Case Using TextInfo Class Build Products Comparision Table/Grid In ASP.Net , Store Custom Objects In Configuration File and many articles in ASP.Net,C#.Net,VB.Net,Grid View,Javascript,jQuery,SQL Server and many other topics.


In this tutorial for explaining purpose i have a textbox to get the html string as input and a button control. While clicking on the button it will strip the html and output the plain text in the next textbox. Below i have mentioned the entire code.

HTML Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <h2>Regular Expression Code to Remove HTML tags in C# </h2>
    <table>
        <tr>
            <td>Input Html</td>
            <td>
                <asp:TextBox ID="txtHtml" runat="server" TextMode="MultiLine">
                </asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnStripHtml" runat="server" OnClick="btnStripHtml_Click"
                    Text="Strip Html" />
            </td>
        </tr>
        <tr>
            <td>Plain Text</td>
            <td>
                <asp:TextBox ID="txtPlainText" runat="server" TextMode="MultiLine">
                </asp:TextBox>
            </td>
        </tr>
    </table>
</div>
</form>
</body>
</html>

Below is the cs code.

C#.Net:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// Include this if it is not already there
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    // Method to strip HTML Tags
    public string StripHtmlTags(string source)
    {
        return Regex.Replace(source, "<.*?>|&.*?;", string.Empty);
    }
    protected void btnStripHtml_Click(object sender, EventArgs e)
    {
        // Getting Input HTML
        string inputHtml = txtHtml.Text;
        // Removing HTML tags including &nbsp; from the input
        string outputText = StripHtmlTags(inputHtml);
        // Assigning plain text output to output textbox
        txtPlainText.Text = outputText;
    }
}

VB.Net:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
' Include this if it is not already there
Imports System.Text.RegularExpressions

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)

    End Sub

    ' Method to strip HTML Tags
    Public Function StripHtmlTags(source As String) As String
        Return Regex.Replace(source, "<.*?>|&.*?;", String.Empty)
    End Function
    Protected Sub btnStripHtml_Click(sender As Object, e As EventArgs)
        ' Getting Input HTML
        Dim inputHtml As String = txtHtml.Text
        ' Removing HTML tags including &nbsp; from the input
        Dim outputText As String = StripHtmlTags(inputHtml)
        ' Assigning plain text output to output textbox
        txtPlainText.Text = outputText
    End Sub
End Class

If you run the application then you will get the below output.
Output of Regular Expression To Strip/Remove Html Tags From String in ASP.Net C#.Net VB.Net
SourceCode:
Source code of Regular Expression To Strip/Remove Html Tags From String in ASP.Net C#.Net VB.Net
If you like this article then share with your friends and comment your valuable feedback.. Happy coding..

Comments

Popular posts from this blog

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     {         sb.Append( "Minus " );         inputNo = -inputNo;     }     string [] words0 = { "" , "One " ,

C# code to send mail using smtp from gmail,yahoo mail and live mail

Introduction: In my previous article I have explained about   How to bind/Unbind events in jQuery . In this article I am going to explain about how to send mail from ASP.Net using gmail,yahoomail and live mail credentials. Explanation: First Include the below namespaces in your code behind file. using System; using System.Net; using System.Net.Mail;

Geckofx Browser in Winform Application

Bored with IE browser in your winform application ? Want to do everything as you doing in your Firefox or Chrome Browser ? Play with automation ? Then here is your choice . Introduction:  GeckoFX is a Windows Forms control written in clean, commented C# that embeds the Mozilla Gecko browser control in any Windows Forms Application. It also contains a simple class model providing access to the HTML and CSS DOM . GeckoFX was originally created by Andrew Young for the fast-growing visual CSS editor, Stylizer . It is now released as open-source under the Mozilla Public License.  You can download it here :   Geckofx 22.0 And the supporting file Xulrunner here :   Xulrunner Files Hope you have downloaded above two files. Here our journey going to start. Create your winform application in visual studio and do the following: right click the toolbox -> Choose items -> Browse the "Geckofx-winforms.dll" and click "yes" for “Load it anyw