Stripping HTML tags using Regular Expressions (RegEx) in C#
Quick method for removing HTML tags from a string, and making it safe for being inside of an attribute. Using a combination of RegEx and a StringBuilder.
var builder = new StringBuilder(Regex.Replace(Input, "<[^>]*>", "", RegexOptions.IgnoreCase));
builder.Replace("<", "");
builder.Replace(">", "");
builder.Replace("\"", "");
var result = builder.ToString();
The way this piece of code works:
1. Remove all any content surrounded by < > brackets.
2. Remove any remaining single < and > brackets.
3. Remove any " characters, to make it safe for being inside of an attribute.
Written By: mycodeshare on March 28, 2009 at 12:00
Submit a comment, suggestion, or additional information about this snippet
If you login or register,
you'll be able to post a comment or provide feedback about this snippet.