Trying to post-process some CSS here and need a regex to select data-attribute selectors in CSS.
It will need to select the following selectors (just the selector but in it's entirety):
[data-attr] {
// some css
}
.cool-class[data-another-attr] {
// some css
}
#id[data-one-more-attr] {
// some css
}
So that would select [data-attr]
, .cool-class[data-another-attr]
and #id[data-one-more-attr]
.
What I have so far (not even close):
/(\[data-*)\w/g
Thanks in advance for your help, RegEx always stumps me.
You can use following regex
((#|\.)?[\w-]+)?(\[data(-\w+)+\])/g
Explanation:
(#|\.)?
: Matches #
or .
literal optionally[\w-]+
: Matches any alphanumeric character with -
any number of times((#|\.)?[\w-]+)?
: Matches CSS id
or class
selector optionally(?
)(\[data(-\w+)+\])
: Matches data
attribute