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,612
  • 4
  • 36
  • 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,353
  • 1
  • 33
  • 47
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
  • 25,215
  • 3
  • 43
  • 60
0

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

SecretDeveloper
  • 3,062
  • 2
  • 29
  • 36