avi dahan
avi dahan

Reputation: 559

Regex For css SelectorText

I'm trying to write a regex to extract nodes ids and classes from css selectorText.

For example:

.linear *, .test-in *, .test-out *, .test *, .test-in-out.casanova#casanova * > #test22

I need a regex to extract only the classes and ids:

linear , test-in , test-out , test , test-in-out , casanova , casanova , test22

so I need a regex to match any string that starts with ./# and ends with space/./# the first occurrence for any of them.

I manage to create this:

(?:^|[\.]).*?([^\s]+(?!\.))

but as you can see it doesn't work properly.

Upvotes: 1

Views: 63

Answers (1)

(?:\.|\#)([_a-zA-Z0-9-\:]*)(?:\b|<|>|\+|~|\[)

This regex will also capture some invalid css selectors such as -_--__abc. But in your case I believe this will solve your problem.

Upvotes: 1

Related Questions