将HTML文本中的标记分为:注释,script,style,以及其他标记分别去掉:
1.去注释,正则为:
output = Regex.Replace(input,@"<!--[^-]*-->",string.Empty,RegexOptions.IgnoreCase);
2.去script,arial; font-size:14px; line-height:21px"> ouput = Regex.Replace(input,@"<script[^>]*?>.*?</script>",RegexOptions.IgnoreCase | RegexOptions.Singleline);
output2 = Regex.Replace(ouput,@"<noscript[^>]*?>.*?</noscript>",RegexOptions.IgnoreCase | RegexOptions.Singleline);
3.去style,@"<style[^>]*?>.*?</style>",arial; font-size:14px; line-height:21px"> 4.去其他HTML标记
result = result.Replace(" "," ");
result = result.Replace(""","\"");
result = result.Replace("<","<");
result = result.Replace(">",">");
result = result.Replace("&","&");
result = result.Replace("<br>","\r\n");
result = Regex.Replace(result,@"<[\s\S]*?>",arial; font-size:14px; line-height:21px"> 以上的代码中大家可以看到,我使用了RegexOptions.Singleline参数,这个参数很重要,他主要是为了让"."(小圆点)可以匹配换行符.如果没有这个参数,大多数情况下,用上面列正则表达式来消除网页HTML标记是无效的.