有效 css 和/或 jQuery 选择器的正则表达式
我需要这样的东西:
Regex validSelector = new Regex("^[.#a-zA-Z0-9_:[] ]+[,><a-zA-Z0-9_~=\"\":[] ]*$");
有人写过正则表达式来验证 jQuery 或 CSS 选择器吗? (CSS3/jQuery1.4)
或者,有人知道我在哪里可以找到正则表达式或正则表达式生成器来验证 jQuery 选择器?(或至少 CSS 选择器?)
我尝试过 sizzle.js bu John Resig 和 CSS 的 w3c 文档 (http://www.w3.org/Style/CSS/) 但最终得到了头痛:(
我想我宁愿问你们中是否有人已经为此编写或使用了一些片段。
谢谢!!
I need something like this :
Regex validSelector = new Regex("^[.#a-zA-Z0-9_:[] ]+[,><a-zA-Z0-9_~=\"\":[] ]*$");
Has anybody written a regular expression for validating jQuery or CSS selectors?
(CSS3/jQuery1.4)
Or, does anybody know where I can find a regex or regex generator to validate a jQuery selector?(or atleast CSS selectors?)
I've tried going through the code of sizzle.js bu John Resig and the w3c docs for css (http://www.w3.org/Style/CSS/) but ended up havin a headache :(
Thought i'd rather ask if any of you has already written or used some snippet for this.
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CSS 3 选择器规范 有一个关于 选择器语法,可用于构建正则表达式。只需从选择器开始,用它的产生式替换每个变量,直到没有变量为止。
The Specification of CSS 3 Selectors has a section about the grammar of selectors that you can use to build the regular expression. Just start with selector and replace each variable with its production until there are no variables left.
正则表达式不能成为出色的解析器。 (它们可能是解析器的重要组成部分。)如果您确实需要这样做,您可能需要实际使用解析器。 StackOverflow 上的此问题的答案可能会有所帮助。
Regular expressions don't make great parsers. (They may make great parts of a parser.) If you really need to do it, you'll probably need to actually use a parser. The answers to this question here on StackOverflow may help there.