function Match(pattern,str) { var step='/'; var keyReg=new RegExp('().*{}+?[]^$\\!','g'); if(typeof pattern!="string"){ throw "pattern 为字符串"; } pattern=pattern.trim(); patterns=pattern.replace(/^\//,'').split(step); compile = str.replace(/^\//, '').split(step); if(patterns.indexOf("**")>-1){ var len=compile.length-patterns.length; var ind=patterns.indexOf("**"); patterns.splice(ind,1); compile.splice(ind,len+1); } var result=""; patterns=patterns.map(function(rule){ var chat=[],start=false,end=false; for (var i = 0, len = rule.length, c ; (i < len) && (c = rule.charAt(i)) ; i++) { switch (c) { case '!':chat.push("!");break; case '.':chat.push(".");break; case '*':chat.push("*");break; case '?':chat.push("?");break; case '+':chat.push("+");break; case '@':chat.push("{1}");break; case '(':start=true;result+=c;break; case ')':end=true;result+=c;if(start&&end&&chat.length>0){ var r=chat.pop(); if(r==='!'){ result="(?!"+result+").*"; }else{ result+=r; } start=false,end=false; };break; default: if(chat.length&&!start){ while(r=chat.shift()){ if(r=='.'){ result+="\\."; }else if(r==='!'){ result+="[^"+c+"]"; c=""; }else{ result+="[^.]"+r; } } start=false,end=false; } result+=c; } } if(chat.length){ while(r=chat.shift()){ if(r=='.'){ result+="\\."; }else{ result+="[^.]"+r; } } start=false,end=false; } return "^"+result+"$"; }); var isSuc=patterns.every(function(rule,index){ var re=new RegExp(rule,"g"),str=""; if(index==patterns.length-1){ str=compile.join('/'); }else{ str=compile.shift(); } return re.test(str) }); return isSuc; } module.exports= Match;