Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#3.5

Replace HTML Tags From text/string Using regex

2.23/5 (9 votes)
12 Dec 2015CPOL 42.2K  
Replace all HTML tags from text/string

Introduction

This tip shows you how to replace HTML tags from text/string using Regular expression.

Using the Code

Pass text/string to input variable. The regular expression will remove HTML tags like <p>, </p>, <body>, </div>, etc. This is case insensitive.

C#
string input = "<b>This is test.</b><p> Enter any text.</p><div> The place is really beautiful.</div><img src=''>";

//To remove tags which are without any attribute
string str1 = Regex.Replace(input, @"(\<(\/)?(\w)*(\d)?\>)", string.Empty);

//To remove all kind of tags -- suggested by codeproject member 'svella'
string str2 = Regex.Replace(input, @"<.*?>", string.Empty);

Console.WriteLine(str1);
Console.WriteLine(str2);
Console.ReadLine();

Points of Interest

  1. Less code compared to string.Replace(), less maintenance.
  2. Even if tomorrow new HTML tags come or it can even remove 3rd party tags which follow HTML tag kind of syntax

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)