过滤HTML 脚本 样式 避免样式冲突

 public static string ToSafeHtmlString(string content)
{
if (content == null)
{
return string.Empty;
}
if (string.IsNullOrWhiteSpace(content))
{
return content;
}
content = Regex.Replace(content, @"<script[^<>]*?>[\s\S]*?</script>", String.Empty, RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<([\w]+?[^<>]*?) (class|style)=""[^""]*?""([^<>]*?)>", "<$1$3>", RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<([\w]+?[^<>]*?) (class|style)='[^']*?'([^<>]*?)>", "<$1$3>", RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<([\w]+?[^<>]*?) (class|style)=[^<>]*?( [^<>]*?|)>", "<$1$3>", RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<frameset[^<>]*?>[\s\S]*?</frameset>", String.Empty, RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<frameset[^<>]*?>", String.Empty, RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<iframe[^<>]*?>[\s\S]*?</iframe>", String.Empty, RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<iframe[^<>]*?>", String.Empty, RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<script[^<>]*?>", String.Empty, RegexOptions.IgnoreCase);
return content;
}



你可能感兴趣的:(html)