1.styleReg:清除样式.如<style>.class{}</style>.全部替换为空.
2.scriptReg和styleReg同样的道理.
3.htmlReg :清除html标签的.输入为<div>aaa</div>,结果为:aaa
4.htmlSpaceReg :html空格 替换为空格
5.spaceReg :把一个以上的空格替换为一个空格.
- public string RemoveHtml(string src)
- {
- Regex htmlReg = new Regex(@"<[^>]+>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
- Regex htmlSpaceReg = new Regex("\\ \\;", RegexOptions.Compiled | RegexOptions.IgnoreCase);
- Regex spaceReg = new Regex("\\s{2,}|\\ \\;", RegexOptions.Compiled | RegexOptions.IgnoreCase);
- Regex styleReg = new Regex(@"<style(.*?)</style>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
- Regex scriptReg = new Regex(@"<script(.*?)</script>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
- src = styleReg.Replace(src, string.Empty);
- src = scriptReg.Replace(src, string.Empty);
- src = htmlReg.Replace(src, string.Empty);
- src = htmlSpaceReg.Replace(src, " ");
- src = spaceReg.Replace(src, " ");
- return src.Trim();
- }