1

I need to remove several thousand comments from an HTML document. The comments are in this form (multi-line):

<p>some HTML</p>
<!--
  FOO
  BAR
  BLAH
-->
<p>more HTML</p>

What regular expression can I use in a find/replace to return this result:

<p>some HTML</p>
<p>more HTML</p>
user2393462435
  • 2,652
  • 5
  • 37
  • 45
  • 1
    duplicate possible http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – KJYe.Name Mar 11 '11 at 15:27

3 Answers3

1

If you only want to remove comments in this particular format (and leave all other comments intact):

replace(/^<\!--.*?-->$/mg, "")

The .*? is non-aggressive match. "m" flag necessary to make it multi-line (so ^ and $ will match the beginning/end of lines respectively).

If you want to remove all comments:

replace(/<\!--.*?-->/g, "")
Stephen Chung
  • 14,497
  • 1
  • 35
  • 48
1

If you have Dreamweaver, the "Clean Up HTML/XHTML" command has an option to remove Non-Dreamweaver comments. That should take care of a problem like this very easily.

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
0

You shouldnt use Regex for this. Try the HTML agility pack instead.
HTML Agility Pack

SecretDeveloper
  • 3,140
  • 2
  • 31
  • 36