验证CSS是否为有效语法 [英] Verify CSS is valid syntax
问题描述
可以在CSS上使用哪些准则,以确保至少在语法上有效.我提出的四项检查确保至少有一个"{",}",;"和:"存在,如果确实存在,则表示"{"和}"的数目匹配,并且:"和;"的数目匹配比赛.我还要检查以确保它以."开头.并以;}"结尾,并带有所有可能的空格组合.
What are some guidelines that could be used on CSS to ensure that it is valid at least in syntax only. The four checks that I came up with were ensuring that at least one "{", "}", ";" and ":" exist and if they do exist that the number of "{" and "}" matches and the number of ":" and ";" match. I also check to make sure it starts with a "." and ends with ";}" with all possible combinations of whitespace.
我还有其他检查可以用来确保它是有效的语法吗?
Are there any other checks I could run on it just to make sure it is valid syntax?
或者更好的是,还有没有人提供可以验证这一点的正则表达式?我是regex的新手,但似乎它能够处理我提到的所有支票.
Or better yet does anyone have a regex available that could verify this? I'm extremely new to regex but it seems like it would be able to handle all the checks I've mentioned.
推荐答案
我能想到的最简单的合适正则表达式是:
The simplest suitable regex that I can think of is:
(([.#]?[a-zA-Z_-0-9]+\ *)+\{([a-zA-Z-])+\ *\:[^;]+;\})+
它不考虑 @charset ...; 指令和注释(\ * ... * \),也不会检查 url(...)中的括号和 content:"..." 中的"..."对,但是您可以根据需要尝试自己添加.
It doesn't consider @charset ...; directives and comments (\* ... *\), also it doesn't check for braces in url(...) and "..." pairs in content: "...", but you can try adding this by yourself if you need.
这篇关于验证CSS是否为有效语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!