Sign in

First, I have never seen "Modifier" separated into "Modifier Name" and "Modifier Value." Where does that come from?

Also, your pattern allows an arbitrary number of dashes, and I would argue that something like ".triple---dash" is invalid. That doesn't seem to be covered in your test cases. Your expression definitely doesn't correctly handle all of the test cases. For instance, the entire set of "trailing dash" test cases should fail, but actually pass.

I landed on this:

^\.([a-z](?:-?[a-z\d])*)(__[a-z\d]+(?:-[a-z\d]+)*)?(--[a-z\d]+(?:-[a-z\d]+)*)?$

This captures the block, element, and modifier as groups 1, 2, and 3, respectively. You can lose the ?: operators, if those capture groups are irrelevant.

Or with your double modifier...

^\.([a-z](?:-?[a-z\d])*)(__[a-z\d]+(?:-[a-z\d]+)*)?(--[a-z\d]+(?:-[a-z\d]+)*){0,2}$

Software engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store