Matthew
Matthew

Reputation: 2246

Regex (JS) — Match any 5-character combination, but ignore 5-character repeat

I struggle with regular expressions, and I've been trying to make one that can match any 5-character combination of 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}

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

I also tried using the non-capturing group (?:), but it didn't work out too well.

Upvotes: 3

Views: 133

Answers (2)

vks
vks

Reputation: 67968

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

Try this.See demo.

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

Upvotes: 5

Avinash Raj
Avinash Raj

Reputation: 174726

You may try the below assertion based regex.

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

Upvotes: 2

Related Questions