正则表达式删除CSS注释 [英] Regular expression to remove CSS comments

查看:197
本文介绍了正则表达式删除CSS注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在php中编写正则表达式以匹配双引号和单引号内的行.实际上,我正在编写用于删除CSS文件中注释行的代码.

I want to write the regular expression in php for matching the line within a double and single quotes. Actually I am writing the code for removing comment lines in css file.

赞:

"/* I don't want to remove this line */"

但是

/* I want to remove this line */

例如:

- valid code /* comment */ next valid code "/* not a comment */" /* this is comment */

预期结果:

- valid code next valid code "/* not a comment */"

请任何人根据我的要求在php中给我一个正则表达式.

Please any one give me a regular expression in php for my requirement.

推荐答案

请执行以下操作:

preg_replace( '/\s*(?!<\")\/\*[^\*]+\*\/(?!\")\s*/' , '' , $theString );

测试用例:

$theString = '- valid code /* comment */ next valid code "/* not a comment */" /* this is comment */';

preg_replace( '/(?!<\")\/\*[^\*]+\*\/(?!\")/' , ' ' , $theString );

# Returns 'valid code next valid code "/* not a comment */" '

修订:2014年11月28日

根据@hexalys的评论,他引用了根据该文章,更新后的正则表达式为:

The updated regular expression, as per that article, is:

preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!' , '' , $theString );

这篇关于正则表达式删除CSS注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆