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

Sort Dictionary Based On Value In Asp.Net And C#.Net | Convert Dictionary into KeyValuePair or KeyValuePair into Dictionary.

In this tutorial i am going to explain about how to sort dictionary object based on value in asp.net and C#.Net or convert unsorted dictionary to sorted dictionary object in C#.Net and VB.Net or Convert Dictionary into KeyValuePair or KeyValuePair into Dictionary.

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

Code to create log files in C#.Net|Asp.Net

Introduction: In my previous article I have explained about how to create, delete and check whether the directory exists using C#.Net . In this article I am going to explain about  How to create log files in C#.Net. Explanation: Log files are useful to track any runtime errors and exceptions in all the applications. Below code will code will get the Message and Pagename as the input and creates the log file in that date. For that first i have imported below two namespaces.