jquery 正则校验重复字符
正则表达式
(\w)[^\1]{0,}\1
jquery实现
//校验重复 function checkRepeated(str) { var reg = /(\w)[^\1]{0,}\1/g; return reg.test(str); } var flag = checkRepeated(course); if (flag) { $.modal.alertError("字符串:"+course+"<br/>是否有重复:"+flag); return false; }
校验效果
或循环遍历校验
//校验重复信息 //var str = "40,42,45,46,42,43,41,40"; function checkRepeated(str) { str = str.substring(0,str.lastIndexOf(",")); var strs = str.split(","); var length = strs.length; for (var i = 0; i < length-1; i++) { for (var j = i+1; j < length; j++) { if (strs[i] == strs[j]) { return true; } } } return false; }