65.9K
CodeProject is changing. Read more.
Home

Replace HTML Tags From text/string Using regex

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.23/5 (9 votes)

Dec 10, 2015

CPOL
viewsIcon

42360

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.

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