简体   繁体   English

正则表达式检查任何字符超过两次

[英]Regular expression to check any character in more than two times

I'm looking for a simple regular expression to match same character in the string more than two times likes following in java script.我正在寻找一个简单的正则表达式来匹配字符串中的相同字符两次以上,就像在 java 脚本中一样。 (Number 1 is three times) 112561 (数字1是三倍)112561

You could check if a group is more than tow times in the array.您可以检查一个组在数组中是否超过两次。

 console.log(/(.).*\1.*\1/.test('12561')); console.log(/(.).*\1.*\1/.test('112561'));

The following regex can be used for this purpose:以下正则表达式可用于此目的:

.*(.).*\1.*\1.*

This will check if a character is repeated more than 2 times in a string.这将检查一个字符是否在字符串中重复超过 2 次。 Note that this also does not use * which is a cause for performance issues in JavaScript.请注意,这也不使用* ,这是 JavaScript 中性能问题的原因。

 console.log(/.+?(.)((?:.)+?\1){1,}/.test('abcZdefZghiZ')) console.log(/.+?(.)((?:.)+?\1){1,}/.test('112561')) console.log(/.+?(.)((?:.)+?\1){1,}/.test('12561'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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