0

Earlier I tried some regex to trim strings between <!-- and --> (included these tags as well), but no success.

Could you give me some informations about the problem?

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Freddiboy
  • 133
  • 10
  • 3
    @Johnsyweb That's just plain wrong. Comments in HTML cannot contain another comment, so you don't go into problems with ambiguous markup - comment is just text ended with "-->" sequence (sequence in any place in comment will end it, you can't escape it) – MBO Feb 08 '13 at 09:31
  • @MBO: while in a comment, "-->" is unambiguously the end of a comment. However, "" onmouseover="alert('uh oh')">Ooh! Move over me!`. Even using regular expressions to parse *comments* is likely to be a bad idea. – Chris Morgan Feb 08 '13 at 10:07
  • @ChrisMorgan Good point, I haven't considered start of comment inside tags... So it's still hard problem. – MBO Feb 08 '13 at 10:09
  • @MBO: at least `` was abolished. – Chris Morgan Feb 08 '13 at 10:14

2 Answers2

2

Regex to match HTML comments:

/<!--.*?-->/s

Explained demo here: http://regex101.com/r/qZ4uP9

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
CSᵠ
  • 10,049
  • 9
  • 41
  • 64
2

To match a comment in text that looks like the rule for XML comments (and very close to the HTML comments rule) you want something like this:

<!--.*?-->

Assuming that your language's RE engine supports non-greedy quantifiers. Removal requires repeatedly matching that across the whole input text and substituting for the empty string; the syntax for that depends on the language in question.

Without non-greedy quantifiers, things get more complex (NB, this is slightly off for HTML comments but you really don't want to learn the details of the difference):

<!--([^-]|-[^-])*-->

Be aware that -- is not something you should encounter in an XML comment unless it is followed by a >; this is part of the nature of XML comments…

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215