3

I'm working on a stylelint rule for modified BEM, which is essentially saying that a single underscore is not allowed, but a double underscore is.

match case:

  • foo__bar

non-match cases:

  • foo_bar

  • foo_bar__baz

I would (ideally) like not just a working code snippet, but an explanation of how a problem like this is best solved using only JavaScript RegEx

Here are the relevant docs for stylelint: https://stylelint.io/user-guide/rules/selector-class-pattern/

Here is a test suite that would need to pass: https://regex101.com/r/NjwOa3/3/tests

Dan Green-Leipciger
  • 3,776
  • 1
  • 19
  • 29
  • What does a Stylelint rule look like? Can you give a template example? **Show us how far you got**. – tadman Jun 19 '19 at 16:35
  • @tadman updated the question – Dan Green-Leipciger Jun 19 '19 at 17:33
  • @tadman I just realized that I what I needed was actually different from what I asked (it needs to also match on no underscores). Should I ask a different question or update this one? The given answer does indeed work for what I asked – Dan Green-Leipciger Jun 19 '19 at 17:44
  • 1
    If you got a satisfactory answer, accept this one and re-post with more specifics. As a note, try and give more examples. Like I'd want to know if `foo___bar` or `__foo_bar` counts or not. – tadman Jun 19 '19 at 17:56

1 Answers1

3

You can try a regular expression like below.

^[^_]*_{2,2}[^_]*$

or try this for matching only the double underscores.

_{2,2}