0

I'm searching a way to replace all html comments from a string like browser does. (multilined and unclosed)

For example, I actually use /(<\!--[\s\S]*?-->)/gim but if the html comment is unclosed, it does not replace it.

Normally, if the comment tag is not closed, comment tag gets everything after open tag...

Is there a way to adapt the regexp (or any other regexp) to do the stuff ? (in JavaScript)

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Jordan
  • 3,776
  • 3
  • 22
  • 28
  • 5
    [This is a bad idea.](http://stackoverflow.com/a/1732454/102937) – Robert Harvey Jul 15 '14 at 22:50
  • 1
    *"but if the html comment is unclosed, it do not replace it"* which seems reasonable, since you don't know where the comment ends. What's the behavior your want to achieve? Either way, you cannot reliably remove comments this way. What if you have an HTML element with attribute value `"--> foo"` for example? – Felix Kling Jul 15 '14 at 22:52
  • I know, that why I escape all other "<>" characters. – Jordan Jul 16 '14 at 09:35

3 Answers3

1

This will mark all comments also the one without end tag: <!-- some text -->

<!--[\s\S]*?(?:-->|$)

This will mark all comments also the one without end tag: <!-- some text //-->

<!--[\s\S]*?(?://-->|$)

This will mark everything from the first <!-- to the very end of the file

<!--[\s\S]*?(?:$)     and regex set to `^$ don't match at line breaks`

This will mark everything from the first <!-- to the end of the line

<!--.*
Andie2302
  • 4,825
  • 4
  • 24
  • 43
  • This regex get the comment entirly, also if the comment is unclosed but it do not get everything after comment opening. Exemple: if I have this HTML code "

    hello world!

    ", the regexp stop before the "p" tag. In HTML if an comment is open is open but not closed, everything is commented, I want to approach this way. But its so close.
    – Jordan Jul 16 '14 at 09:45
  • @Jordan The regex stops before the

    tag because the --> marks the end of the comment.

    – Andie2302 Jul 16 '14 at 11:57
  • Sorry, i meant " – Jordan Jul 16 '14 at 13:09
  • I finally found a regexp that work: "(<\!--[\s\S]*?(?:-->|$))/gi". I'm gonna 'answer my own question'. Thanks for your help. – Jordan Jul 16 '14 at 22:52
0

I must agree that using regex like this is not good practice and you shouldn't do it... here's why.

Buuuut, as a matter of understanding regex better, you can make something optional like this:

/(<\!--[\s\S]*?(?:-->)?)/gim

  1. I wrapped --> in parenthesis to group it together

  2. I put a ? after that group to make it optional

  3. (not necessary) I put ?: inside of the group to keep the regex engine from saving a back reference... it's a performance nuance.

Community
  • 1
  • 1
Ryan Wheale
  • 26,022
  • 8
  • 76
  • 96
0

Thanks to @Andie2302 for the help.

This regexp /<!--[\s\S]*?(?:-->|$)/gi work find.

Do not use the flag m!

Jordan
  • 3,776
  • 3
  • 22
  • 28