function check( str ) {
while( str.length ) {
// 取字符串的第一个字符,在剩余的字符中查找,如果找到,说明有重复
if( str.slice(1).indexOf( str.charAt( 0 ) ) > -1 ) {
return true;
}
// 如果没找到,把字符串去掉第一个字符,继续查找
str = str.slice(1);
}
return false;
}
check( 'abcdefg' ); // return false
check( 'abcdefta' ); // return true
还可以通过正则表达式来实现,就一行代码:
function check( str ) {
return /(.).*?\1/.test( str );
}