Adam Bernot
1 min readOct 8, 2020

--

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}$

--

--

Adam Bernot
Adam Bernot

Written by Adam Bernot

0 Followers

Software engineer

Responses (1)