简体   繁体   中英

Using Regex to remove css comments

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 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.

使用链接问题中的正则表达式,如下所示:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM