Strip HTML Tags In C#
Posted by VanDamme Associates
on Wednesday, October 26, 2005
While working on our content manager, I've found that at times I want to remove HTML that a user may have entered into a field where HTML is not allowed. This is pretty easy to do using regular expressions. Here's how I implemented HTML tag removal in C#:
using System.Text.RegularExpressions;
public string Strip(string text)
{
return Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
}
That's it! If you call the Strip() function and pass it an HTML-littered string then it will return an HTML-free string!
Care to Comment?