How can I remove comments from CSS using Regex.Replace()
?
Note - I\'m not able to use the regex mentioned here in C# - Regular expression to remove CSS comm
Very late reply but thought it will be useful for some
"(?:/*(.|[\r\n])?/)|(?:(?([^)])//.)"
This will help removing css comments both singleline and multiline.
That would be normally enough (assuming cssLines
is a string containing all lines of your CSS file):
Regex.Replace(cssLines, @"/\*.+?\*/", string.Empty, RegexOptions.Singleline)
Please note that the Singleline
option will allow to match multi-line comments.
I wonder if the following version of Maxim's solution would be faster.
"/\*[^*]*.*?\*/"
As the discussion shows this will also eliminate comments within string literals.
Use the regex from the linked question like so:
var rx = new Regex(@"(?<!"")\/\*.+?\*\/(?!"")");