Regex (JS) - match any combination of 5 characters, but ignore 5-character repeat

I am struggling with regexes and I am trying to create one that can match any combination of 5 characters X

and O

, but ignore it if it repeats X

or O

EXACTLY 5 times.

This is what I came up with:

X{1,4}|O{1,4}
X|O{1,4}

      

these expressions match (I want it to ignore XXXXX and OOOOO): enter image description here

I also tried using a non-capturing group, (?:)

but that didn't work too well.

+3


source to share


2 answers


^(?!(.)\1+$)[XO]{5}$

      

Try it. Check out the demo.



https://regex101.com/r/uK9cD8/1

+5


source


You can try the following assertion based regex.



^(?!(?:X{5}|O{5})$)(?=.*X)(?=.*O)[XO]{5}$

      

+2


source







All Articles