I have the following code:
source = Regex.Replace(source, "<!--.*?-->", string.Empty, RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
…and it works fine, except that it also strips away conditional comments:
<!--[if IE]>???<![endif]-->
Can someone tell me how to update the regex so it doesn’t remove conditional comments?
You can use negative lookaheads and negative look behinds:
Should only match
<!--not followed by [ and any-->not preceded by ], and of course, the .*? matches the text inbetween.Tested using Derek Slager’s very useful and quick RegEx tester:
Source Text:
Pattern:
Result:
Found 1 match: