开发者

Using Regex to remove css comments

开发者 https://www.devze.com 2023-02-16 09:48 出处:网络
How can I remove comments from CSS using Regex.Replace()? Note - I\'m not able to use the regex mentioned here in C# - Reg开发者_Python百科ular expression to remove CSS comments.That would be normall

How can I remove comments from CSS using Regex.Replace()?

Note - I'm not able to use the regex mentioned here in C# - Reg开发者_Python百科ular expression to remove CSS comments.


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.


Use the regex from the linked question like so:

var rx = new Regex(@"(?<!"")\/\*.+?\*\/(?!"")");


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.


Very late reply but thought it will be useful for some

"(?:/*(.|[\r\n])?/)|(?:(?([^)])//.)"

This will help removing css comments both singleline and multiline.
0

精彩评论

暂无评论...
验证码 换一张
取 消