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

Puzzles for kids - 12 Days Of Christmas

According to the traditional song, on the first day of Christmas (25th December), my true love sent to me: . A partridge in a pair tree On the second day of Christmas (26th December), my true love sent to me THREE presents: . Two turtle doves . A partridge in a pear tree On the third day of Christmas (27th December and so on) my true love sent to me SIX presents: . Three French hens . Two turtle doves . A partridge in a pear tree This carries on until the the twelfth day of Christmas, when my true love sends me: Twelve drummers drumming Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying Five gold rings Four calling birds Three French hens Two turtle doves A partridge in a pear tree After the twelve days of Christmas are over, how many presents has my true love sent me altogether? Our Solution: 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 55 + 66 + 78 = 364 presents Which is really interesting when you think ...

Asp.Net DataTable Manipulation - Add, Update,Delete & Sort DataTable in C#.Net & VB.Net

In the asp.net developer's life it is very common to come across the datatable manipulation. So here i have decided to explain the datatable manipulation like adding records to datatable, editing and updating datatable records,deleting records from datatable based on the condition and finally sorting datatable based on columns.