/// <summary> ///Remove HTML tags (completely remove html\css\script code with regular)/// </summary> /// <param name= "htmlstring" >including the HTML source code</param> /// <returns>text that has been removed</returns> Public Static stringNohtml ( This stringhtmlstring) { //Delete Scripthtmlstring = Regex.Replace (htmlstring,@"<script[^>]*?>.*?</script>","", regexoptions.ignorecase); //Delete HTMLhtmlstring = Regex.Replace (htmlstring,@"< (. [ ^>]*) >","", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@"([\ r \ n]) [\s]+","", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@" -","", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@"<!--. *","", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@"& (quot| #34);","\"", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@"& (amp| #38);","&", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@"& (lt| #60);","<", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@"& (gt| #62);",">", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@"& (nbsp| #160);"," ", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@"& (iexcl| #161);","\xa1", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@"& (cent| #162);","\xa2", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@"& (pound| #163);","\xa3", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@"& (copy| #169);","\xa9", regexoptions.ignorecase); Htmlstring= Regex.Replace (htmlstring,@"(\d+);","", regexoptions.ignorecase); Htmlstring.replace ("<",""); Htmlstring.replace (">",""); Htmlstring.replace ("\ r \ n",""); Htmlstring=HttpContext.Current.Server.HtmlEncode (htmlstring). Trim (); returnhtmlstring; }
View Code
C # replaces HTML tags with regular expressions