Skip to content

Instantly share code, notes, and snippets.

@swade1987
Created April 28, 2015 10:24
Show Gist options
  • Save swade1987/671107ac7e59a46c21e9 to your computer and use it in GitHub Desktop.
Save swade1987/671107ac7e59a46c21e9 to your computer and use it in GitHub Desktop.
Remove unwanted HTML tags from string
/// <summary>
/// Reference: http://stackoverflow.com/questions/307013/how-do-i-filter-all-html-tags-except-a-certain-whitelist
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public string RemoveUnwantedTags(string source)
{
const string acceptable = "b|i|p";
const string stringPattern = @"</?(?(?=" + acceptable + @")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:(["",']?).*?\1?)?)*\s*/?>";
return Regex.Replace(source, stringPattern, "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment