Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since comments cannot be nested in HTML, a regex can do the job, in theory. Still, using some kind of parser would be the better choice, especially if your input is not guaranteed to be well-formed.</p> <p>Here is my attempt at it. To match only normal comments, this would work. It has become quite a monster, sorry for that. I have tested it quite extensively, it seems to do it well, but I give no warranty.</p> <pre class="lang-none prettyprint-override"><code>&lt;!--(?!\s*(?:\[if [^\]]+]|&lt;!|&gt;))(?:(?!--&gt;).)*--&gt; </code></pre> <p>Explanation:</p> <pre class="lang-none prettyprint-override"><code>&lt;!-- #01: "&lt;!--" (?! #02: look-ahead: a position not followed by: \s* #03: any number of space (?: #04: non-capturing group, any of: \[if [^\]]+] #05: "[if ...]" |&lt;! #06: or "&lt;!" |&gt; #07: or "&gt;" ) #08: end non-capturing group ) #09: end look-ahead (?: #10: non-capturing group: (?!--&gt;) #11: a position not followed by "--&gt;" . #12: eat the following char, it's part of the comment )* #13: end non-capturing group, repeat --&gt; #14: "--&gt;" </code></pre> <p>Steps #02 and #11 are crucial. #02 makes sure that the following characters do not indicate a conditional comment. After that, #11 makes sure that the following characters do not indicate the end of the comment, while #12 and #13 cause the actual matching.</p> <p>Apply with "global" and "dotall" flags.</p> <p>To do the opposite (match only conditional comments), it would be something like this:</p> <pre class="lang-none prettyprint-override"><code>&lt;!(--)?(?=\[)(?:(?!&lt;!\[endif\]\1&gt;).)*&lt;!\[endif\]\1&gt; </code></pre> <p>Explanation:</p> <pre class="lang-none prettyprint-override"><code>&lt;! #01: "&lt;!" (--)? #02: two dashes, optional (?=\[) #03: a position followed by "[" (?: #04: non-capturing group: (?! #05: a position not followed by &lt;!\[endif\]\1&gt; #06: "&lt;![endif]&gt;" or "&lt;![endif]--&gt;" (depends on #02) ) #07: end of look-ahead . #08: eat the following char, it's part of the comment )* #09: end of non-capturing group, repeat &lt;!\[endif\]\1&gt; #10: "&lt;![endif]&gt;" or "&lt;![endif]--&gt;" (depends on #02) </code></pre> <p>Again, apply with "global" and "dotall" flags.</p> <p>Step #02 is because of the "downlevel-revealed" syntax, see: <a href="http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx" rel="noreferrer">"MSDN - About Conditional Comments"</a>.</p> <p>I'm not entirely sure where spaces are allowed or expected. Add <code>\s*</code> to the expression where appropriate.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload