Regular Expression for valid css and/or jQuery selector


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 <a href="http://sizzlejs.com/" rel="nofollow">sizzle.js</a> 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.



The <a href="http://www.w3.org/TR/css3-selectors" rel="nofollow">Specification of CSS 3 Selectors</a> has a section about the <a href="http://www.w3.org/TR/css3-selectors/#w3cselgrammar" rel="nofollow">grammar of selectors</a> that you can use to build the regular expression. Just start with <em>selector</em> and replace each variable with its production until there are no variables left.


Regular expressions don't make great <em>parsers</em>. (They may make great <em>parts</em> of a parser.) If you really need to do it, you'll probably need to actually use a parser. The <a href="https://stackoverflow.com/questions/512720/is-there-a-css-parser-for-c" rel="nofollow">answers to this question here</a> on StackOverflow may help there.


  • Targeting class name iterations with JS
  • Reverse Zen Coding
  • AS2: Calculate days between two dates
  • Search feature with multiple criteria - PHP/MySQL
  • Scons: create late targets
  • Is this hack a defined behavior for T4
  • ValidationResult Returned From IValidatableObject.Validate Is Not Localized
  • iOS Validation failed -> Bundle is invalid (Xcode 6.2, iOS 8.2)
  • Passing and ArrayList through intent
  • jQuery - how to validate a date of birth using jQuery Validation plugin?
  • Textbox validation in jquery
  • ng-repeat not working with table but works with list
  • css font-size and line-height not matching the baseline
  • How to replace TouchesBegan with UIGestureRecognizer
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • Efficient User-Agent Regex to find Safari in Python
  • How to 'create temp table as select' in Slick?
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • JqueryMobile Popup menu is not working
  • Jquery popup on mouse over of calendar control
  • What is the purpose of TaskExecutor in spring?
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • Zurb Foundation _global.scss meta styles for js?
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Extracting HTML between tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Regex thinks I'm nesting, but I'm not
  • Validaiting emails with Net.Mail MailAddress
  • Why winpcap requires both .lib and .dll to run?
  • QuartzCore.framework for Mono Develop
  • RestKit - RKRequestDelegate does not exist
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • java string with new operator and a literal