Created
April 28, 2015 10:24
-
-
Save swade1987/671107ac7e59a46c21e9 to your computer and use it in GitHub Desktop.
Remove unwanted HTML tags from string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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