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)

3
  • 5
    This is a bad idea. Commented Jul 15, 2014 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? Commented Jul 15, 2014 at 22:52
  • I know, that why I escape all other "<>" characters. Commented Jul 16, 2014 at 9:35

3 Answers 3

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

<!--.*
Sign up to request clarification or add additional context in comments.

4 Comments

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 "<!-- I'm a comment --><p>hello world!</p>", 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 The regex stops before the <p> tag because the --> marks the end of the comment.
Sorry, i meant "<!-- I'm a comment \n<p>hello world!</p>". The regexp correctly get the "p" tag if there is no linebreaks between.
I finally found a regexp that work: "(&lt;\!--[\s\S]*?(?:--&gt;|$))/gi". I'm gonna 'answer my own question'. Thanks for your help.
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.

1 Comment

Its only replacing the start of the comment "<!--" the rest is unchanged.
0

Thanks to @Andie2302 for the help.

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

Do not use the flag m!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.