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=''>";
string str1 = Regex.Replace(input, @"(\<(\/)?(\w)*(\d)?\>)", string.Empty);
string str2 = Regex.Replace(input, @"<.*?>", string.Empty);
Console.WriteLine(str1);
Console.WriteLine(str2);
Console.ReadLine();
Points of Interest
- Less code compared to
string.Replace()
, less maintenance. - Even if tomorrow new HTML tags come or it can even remove 3rd party tags which follow HTML tag kind of syntax