Remove Html Comments from a HTML doucment using Regex

March 24, 2009 at 3:56 PMkevinbosch
If you ever wanted to strip the html comments out of a HTML page here is a function that does just that:
        public static string RemoveHtmlComments(string input)
        {
            if (!string.IsNullOrEmpty(input))
            {
                // using a non greedy regex to get all the html or xml style comments out of the text 
                Regex htmlCommentStripper = new Regex("().)*-->)", RegexOptions.Singleline); 
                return htmlCommentStripper.Replace(input, string.Empty);
            }
            else
            {
                // the string is null or is empty so just return the string as is..
                return input;
            }
        }


Examples of usage can be found at: http://www.codeassociate.com/caapi/html/M_CA_Common_Text_HtmlHelper_RemoveHtmlComments.htm

Posted in:

Tags:

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading