日期:2014-05-16 浏览次数:20337 次
var getChecker = function(word){ var r = new RegExp(( '([' + '\\\\' + word.split('').join('\\\\') + '])' + '.*?\\1' ), 'i'); return { check : function(s){ return !r.test(s); } }; }; var test = function(units, fn, result){ var i, len, unit; for(i=0, len=units.length; i<len; i++){ unit = units[i]; try{ if( fn(unit) != result ){ console.error('not passed!'); return; } }catch(e){ console.error('not passed!'); return; } } console.log('passed'); }; var checker = getChecker('ABSNC'); test('AB BA AS SA ABS BAS SBA'.split(/\s+/), checker.check, true); test('AA BB ABA BAA'.split(/\s+/), checker.check, false);
------解决方案--------------------
一个弄不来,用两个试试:
var s='AB BA AS SA ABS BAS SBA AA BB ABA BAA'.split(/\s+/); for(var i=s.length-1;i>=0;i--)alert((!/([ABSNC]).*\1/gi.test(s[i]) && /^[ABSNC][ABSNC]+$/i.test(s[i]))+' '+s[i])
------解决方案--------------------
表达式:
!/[^ABSNC]|(.)(?=.*\1)/g.test(你要检测的字符串)
示例:
<script> var str = "AB BA AS SA ABS BAS SBA AA BB ABA BAA ABQ BWA ATS SAG"; var arr = str.split(" "); for (var i=0; arr[i]; i++) { alert(arr[i] + ":" + !/[^ABSNC]|(.)(?=.*\1)/g.test(arr[i])); } </script>