简体   繁体   中英

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): 在此输入图像描述

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

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

Try this.See demo.

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

您可以尝试以下基于断言的正则表达式。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM