Javascript 如何确定字符串是否包含重复字母序列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6176684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine if a string contains a sequence of repeated letters
提问by vetri02
Using JavaScript, I need to check if a given string contains a sequence of repeated letters, like this:
使用 JavaScript,我需要检查给定的字符串是否包含一系列重复的字母,如下所示:
"aaaaa"
“啊啊啊”
How can I do that?
我怎样才能做到这一点?
回答by Eric
Use regular expressions:
使用正则表达式:
var hasDuplicates = (/([a-z])/i).test(str)
Or if you don't want to catch aA
and the likes
或者如果你不想抓住aA
和喜欢
var hasDuplicates = (/([a-zA-Z])/).test(str)
Or, if you've decided you want to clarify your question:
或者,如果您决定要澄清您的问题:
var hasDuplicates = (/^([a-zA-Z])+$/).test(str)
回答by melhosseiny
You can use this function:
您可以使用此功能:
function hasRepeatedLetters(str) {
var patt = /^([a-z])+$/;
var result = patt.test(str);
return result;
}
回答by DeveloperX
Try using this
尝试使用这个
checkRepeat = function (str) {
var repeats = /(.)/;
return repeats.test(str)
}
Sample usage
示例用法
if(checkRepeat ("aaaaaaaa"))
alert('Has Repeat!')
回答by wong2
function check(str) {
var tmp = {};
for(var i = str.length-1; i >= 0; i--) {
var c = str.charAt(i);
if(c in tmp) {
tmp[c] += 1;
}
else {
tmp[c] = 1;
}
}
var result = {};
for(c in tmp) {
if(tmp.hasOwnProperty(c)) {
if(tmp[c] > 1){
result[c] = tmp[c];
}
}
}
return result;
}
then you can check the result to get the repeated chars and their frequency. if result is empty, there is no repeating there.
然后您可以检查结果以获取重复的字符及其频率。如果结果为空,则没有重复。
回答by Billy
This will check if the string has repeats more than twice:
这将检查字符串是否重复两次以上:
function checkStr(str) {
str = str.replace(/\s+/g,"_");
return /(\S)({2,})/g.test(str);
}
回答by user2364729
var char = "abcbdf..,,ddd,,,d,,,";
var tempArry={};
for (var i=0; i < char.length; i++) {
if (tempArry[char[i]]) {
tempArry[char[i]].push(char[i]);
} else {
tempArry[char[i]] = [];
tempArry[char[i]].push(char[i]);
}
}
console.log(tempArry);
This will even return the number of repeated characters also.
这甚至还会返回重复字符的数量。
回答by Fernando Carvajal
I solved that using a for loop, not with regex
我使用for 循环解决了这个问题,而不是使用正则表达式
//This check if a string has 3 repeated letters, if yes return true, instead return false
//If you want more than 3 to check just add another validation in the if check
function stringCheck (string) {
for (var i = 0; i < string.length; i++)
if (string[i]===string[i+1] && string[i+1]===string[i+2])
return true
return false
}
var str1 = "hello word" //expected false
var str2 = "helllo word" //expredted true
var str3 = "123 blAAbA" //exprected false
var str4 = "hahaha haaa" //exprected true
console.log(str1, "<==", stringCheck(str1))
console.log(str2, "<==", stringCheck(str2))
console.log(str3, "<==", stringCheck(str3))
console.log(str4, "<==", stringCheck(str4))