Skip to content

Instantly share code, notes, and snippets.

@titanew
Last active December 17, 2015 14:29
Show Gist options
  • Save titanew/5624479 to your computer and use it in GitHub Desktop.
Save titanew/5624479 to your computer and use it in GitHub Desktop.
判断字符串中是否还有重复的字母或者数字。
//一种用普通的字符串的方法,另外一种用正则,还是正则的扩展性高一点。。
//方法一:字符串方法
function ifRepeat(str) {
var ss = str;
for (var i = 0; i < ss.length; i++) {
var cha = ss.charAt(i);
var sub = ss.substring(0,i)+ss.substring(i+1);
if (sub.indexOf(cha)!=-1) {
console.log("有重复");
return;
};
};
console.log("无重复");
};
ifRepeat("1234567890qwertyuioplkjhgfdsazxcvbnm")
//方法二:正则
function ifRepeat(str){
var reg = /(?:^|)(\S{1}).*\1/g;
reg.test(str)?console.log("有重复"):console.log("无重复");
}
ifRepeat("1234567890qwertyuioplkjhgfdsazxcvbnm")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment