What would be the most efficient way of parsing a css selector input string, that features any combination of:
[key=value]
: attributes, 0 to * instances#id
: ids, 0 to 1 instances.class
: classes, 0 to * instancestagName
: tag names, 0 to 1 instances (found at start of string only)
(note: '*
', or other applicable combinator could be used in lieu of tag?)
Such as:
div.someClass#id[key=value][key2=value2].anotherClass
Into the following output:
['div
','.someClass
','#id
','[key=value]
','[key2=value2]
','.anotherClass
']
Or for bonus points, into this form efficiently (read: a way not just based on using str[0] === '#'
for example):
{
tags : ['div'],
classes : ['someClass','anotherClass'],
ids : ['id'],
attrs :
{
key : value,
key2 : value2
}
}
(note removal of # . [ = ]
)
I imagine some combination of regex and .match(..)
is the way to go, but my regex knowledge is nowhere near advanced enough for this situation.
Many thanks for your help.