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();
- }