CSS标识符的允许字符 [英] Allowed characters for CSS identifiers

查看:168
本文介绍了CSS标识符的允许字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CSS标识符 id $ c>?



有没有正规表达式可以用来验证?是不是浏览器不可知?

解决方案

字符集无所谓。允许的字符更重要。请检查 CSS规范。以下是相关的引用: 在bss中,标识符(包括 selectors )只能包含字符 [a-zA-Z0-9] code>和ISO 10646字符 U + 00A1 和更高,加上连字符( - )和下划线( _ );他们不能以数字开头,或者连字符后跟数字。标识符还可以包含转义字符和任何ISO 10646字符作为数字代码(见下一项)。例如,标识符B& W?可以写为B \& W \?B \26 W \3F


更新:对于正则表达式问题,您可以找到语法此处

  ident  - ?{nmstart} {nmchar} * 

其中包含以下部分:

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

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

  String h =[0-9a-f ]; 
String unicode =\\\\ {h} {1,6}(\\r\\\\
| [\\t\\r\\\ \\ n\\f])?。replace({h},h);
String escape =({unicode} | \\\\ [^ \\r\\\\
\\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},nonascii).replace({escape},escape);
String ident = - ?{nmstart} {nmchar} *。replace({nmstart},nmstart).replace({nmchar},nmchar);

System.out.println(ident); //完整的正则表达式。

更新2 :哦,好吧,我想你可以怎么/在哪里做 str_replace


What are the (full) valid / allowed charset characters for CSS identifiers id and class?

Is there a regular expression that I can use to validate against? Is it browser agnostic?

解决方案

The charset doesn't matter. The allowed characters matters more. Check the CSS specification. Here's a cite of relevance:

In CSS, identifiers (including element names, classes, and IDs in selectors) 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".

Update: As to the regex question, you can find the grammar here:

ident      -?{nmstart}{nmchar}*

Which contains of the parts:

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]

This can be translated to a Java regex as follows (I only added parentheses to parts containing the OR and escaped the backslashes):

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}", nonascii).replace("{escape}", escape);
String ident = "-?{nmstart}{nmchar}*".replace("{nmstart}", nmstart).replace("{nmchar}", nmchar);

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

Update 2: oh, you're more a PHP'er, well I think you can figure how/where to do str_replace?

这篇关于CSS标识符的允许字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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