正则表达式匹配CSS类名称 [英] Regex to match a CSS class name

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

问题描述

我想写一个匹配有效的CSS类名结构的正则表达式。我到目前为止:

I am trying to write a regex that matches a valid CSS class name structure. I have this so far:

$pattern = "([A-Za-z]*\.[A-Za-z]+\s*{)";

$regex = preg_match_all($pattern, $html, $matches);



However, a class name can be in the following formats that my regex won't match:

p.my_class{
}
p.thisclas45{
}

这些只是一些情况,我已经找到了如何在一个样式块中命名一个类的规则,找不到任何东西。任何人都知道类命名约定的规则在哪里?

These are just some cases, I've looked around to find the rules of how you can name a class in a style block but couldn't find anything. Anyone know where the rules for the class naming conventions are?

是否还有其他情况需要考虑?你将使用什么正则表达式来匹配类名?

Are there any more cases that I need to consider? What regex would you use to match a class name?

我已经使用PHP DOM Document类将其缩小为样式块。

I have already narrowed it down to a style block using the PHP DOM Document class.

推荐答案

请查看 http://www.w3.org/TR/CSS21 /grammar.html#scanner

根据此语法和帖子什么字符在CSS类名称中有效?这应该是扫描css类的正确模式:

According to this grammar and the post What characters are valid in CSS class names? this should be the right pattern to scan for css classes:

\.-?[_a-zA-Z]+[_a-zA-Z0-9-]*\s*\{

注意:标记名称不需要作为css中类的前缀。只是 .hello {border:1; } 也有效。

Note: Tag names are not required as prefix for classes in css. Just .hello { border: 1; } is also valid.

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

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