正则表达式用于识别 CSS 文件中没有内容的类/ID
我正在更新系统中的一些旧 CSS 文件,并且我们有很多空类,它们只是占用了文件中的空间。我很想学习如何编写正则表达式,但我就是不明白。我希望我越多地接触他们(加上一些更有凝聚力的解释),我最终就会越能理解他们。
问题
也就是说,我正在寻找一个表达式来识别后跟“{”的文本(有些之间有空格,有些没有),并且之间是否没有字母或数字该括号和“}”(空格不计算在内),它将被识别为匹配字符串。
我想我可以在运行正则表达式之前删除文档中的空格,但我不想更改文本的基本结构。我希望将其返回到一个大的
解释字符及其含义的奖励积分,以及在没有任何文本或数字的情况下识别副本中的行的表达方式。我可能会在 PHP 脚本中使用最终的表达式。
tl;dr:
要匹配的正则表达式:
.a_class_or #an_id {
/* if there aren't any alphanumerics in here,
this should be a matching line of text */
}
I'm in the process of updating some old CSS files in our systems, and we have a bunch that have lots of empty classes simply taking up space in the file. I'd love to learn how to write Regular expressions, but I just don't get them. I'm hoping the more I expose myself to them (with a little more cohesive explanation), the more I'll end up understanding them.
The Problem
That said, I'm looking for an expression that will identify text followed by a '{' (some have spaces in between, and some do not) and if there are no letters or numbers between that bracket and '}' (spaces don't count), it will be identified as a matching string.
I suppose I can trim the whitespace out of the doc before I run a regular expression through it, but I don't want to change the basic structure of the text. I'm hoping to return it into a large <textarea>
.
Bonus points for explaining the characters and their meanings, and also an expression identifying lines in the copy without any text or numbers, as well. I will likely use the final expression in PHP script.
tl;dr:
Regular Expression to match:
.a_class_or #an_id {
/* if there aren't any alphanumerics in here,
this should be a matching line of text */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个好的开始。在 PHP 中测试。
现在把它分成几部分;
这部分与选择器相匹配。 # 用于 ID,\w 匹配字母数字和下划线,句点用于类。然后我为一些高级 css 选择器添加了 [ ] = ^ 和 ~ ,但如果它们经常使用,我会感到惊讶。
第二部分寻找大括号内的空白区域。我们这里有空格、换行符、回车符和制表符等特殊字符。如果还有别的东西就不会匹配了。
如果您有兴趣,这里是我用来在 PHP 中测试它的代码。
Heres a good start on one. Tested in PHP.
Now breaking it into parts;
This part matches the selector. # is for IDs, \w matches alphanumerics and underscores, period is for classes. Then I threw in [ ] = ^ and ~ for some advanced css selectors, but I'd be surprised if they get used very often.
The second part looks for an empty space inside of curly brackets. We've got special characters here for spaces, newlines, returns, and tabs. If there is anything else it won't match.
And here is the code I used to test it in PHP, if you're interested.
这将匹配 { 和 } 之间的零个或多个空格。
您必须转义 { 和 },因为它们是正则表达式中的特殊字符。
\s - 表示任何空白字符,包括制表符、换行符等...
\s* - 任意数量的空白
This would match for zero or more whitespace between { and }
You have to escape both { and }, as they are special chars in RegEx.
\s - means any whitespace char including tab, new line etc...
\s* - any number of whitespace
我不推荐正则表达式,因为我真的怀疑 CSS 是否是一种常规语言 。
下载 PHP 版 CSS Tidy - 我自己花了 5 分钟完成并测试了它。效果很好。
这是输出
I don't recommend regular expressions for this, since I really doubt CSS is a regular language anyway.
Download CSS Tidy for PHP - I just did it myself in 5 minutes and tested it. Works great.
Here's the output