I want to parse this CSS Selector (and others of a similar form):
div.class1#myid.class2[key=value]
and have it match “.class1” and “.class2” but I can’t figure out what regex to use..
example: http://www.rubular.com/r/3dxpzyJLeK
In an ideal world, I’d also want to extract the:
- type (i.e. div)
- class (i.e. a list of classes)
- id (i.e myid)
- key (i.e. key)
- operator (i.e. =)
- value (i.e. value)
but I can’t get the basics going!
Any help would be massively appreciated 🙂
Thanks!
Thanks all very much for your suggestions and help. I tied it all together into the following two Regex Patterns:
This one parses the CSS selector string (e.g. div#myid.myclass[attr=1,fred=3]) http://www.rubular.com/r/2L0N5iWPEJ
And this one does the attributes (e.g. [link,key~=value]) http://www.rubular.com/r/2L0N5iWPEJ:
A couple of things to note:
1) This parses attributes using comma delimitation (since I am not using strict CSS).
2) This requires patterns take the format: tag, id, classes, attributes
The first regex does tokens, so the whitespace and ‘>’ separated parts of a selector string. This is because I wanted to use it to check against my own object graph 🙂
Thanks again!