[英]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. 我正在努力使用正则表达式,我一直在尝试制作一个可以匹配任何5个字符的X
和O
组合,但如果它重复X
或O
完全重复5次,则忽略它。
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): 那些表达式匹配(我希望它忽略XXXXX和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 https://regex101.com/r/uK9cD8/1
您可以尝试以下基于断言的正则表达式。
^(?!(?:X{5}|O{5})$)(?=.*X)(?=.*O)[XO]{5}$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.