CSS标识符的允许字符

前端之家收集整理的这篇文章主要介绍了CSS标识符的允许字符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
CSS标识符id和类的(完整)有效/允许的字符集字符是什么?

是否有正则表达式,我可以用来验证?它是浏览器不可知吗?

解决方法

字符集没有关系。允许的字符更重要。检查 CSS specification.这里有一个相关的引用:

In CSS,identifiers (including element names,classes,and IDs in 07001) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A1 and higher,plus the hyphen (-) and the underscore (_); they cannot start with a digit,or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance,the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

更新:至于正则表达式问题,你可以找到语法here

ident      -?{nmstart}{nmchar}*

其中包含的部分:

nmstart    [_a-z]|{nonascii}|{escape}
nmchar     [_a-z0-9-]|{nonascii}|{escape}
nonascii   [\240-\377]
escape     {unicode}|\\[^\r\n\f0-9a-f]
unicode    \\{h}{1,6}(\r\n|[ \t\r\n\f])?
h          [0-9a-f]

这可以转换为Java正则表达式如下(我只添加括号到包含OR和转义反斜杠的部分):

String h = "[0-9a-f]";
String unicode = "\\\\{h}{1,6}(\\r\\n|[ \\t\\r\\n\\f])?".replace("{h}",h);
String escape = "({unicode}|\\\\[^\\r\\n\\f0-9a-f])".replace("{unicode}",unicode);
String nonascii = "[\\240-\\377]";
String nmchar = "([_a-z0-9-]|{nonascii}|{escape})".replace("{nonascii}",nonascii).replace("{escape}",escape);
String nmstart = "([_a-z]|{nonascii}|{escape})".replace("{nonascii}",escape);
String ident = "-?{nmstart}{nmchar}*".replace("{nmstart}",nmstart).replace("{nmchar}",nmchar);

System.out.println(ident); // The full regex.

更新2:哦,你更多是一个PHP’er,我想你可以计算如何/在哪里做str_replace?

猜你在找的CSS相关文章