首頁  >  文章  >  web前端  >  JavaScript中兩個字串的匹配

JavaScript中兩個字串的匹配

高洛峰
高洛峰原創
2016-11-26 14:42:171206瀏覽

工作中遇到一個問題,兩個字符串匹配,要求:每個字符串中最多含有一個*,?可以無限多個

*代表一個任意長度的字符串,而?則代表一個字符

要求可以提示兩個衝突



用Javascript實作程式碼如下:

[javascript] 
function checkMarchX() 
    { 
        var str1 = document.getElementById('str1').value; 2').value; 
        var str1XPosition = str1.indexOf(' *'); 
        var str2XPosition = str2.indexOf('*'); 
        if(str1XPosition!=-1 & str     if(str1XPosition!=-1 & str
            var position = str1XPosition>str2XPosition?str2XPosition :str1XPosition;//success 
            if(position!=0) 
         Str1 = str1.substring(0,position); 
                var patbeforeStr2 = str   foreStr1,patbeforeStr2 )) 
                { 
                            //接著對應後半部進行測試 
                               var str2XBackPosition = str2. length-str2XPosition-1; 
                    var backposition = = str.            if (backposition==0) 
                     alert(str1+'與'+str2+"衝突"); 
                    }                    { 
               patbackStr1 = str1.substring(str1.length-backposition,str1.length); 
                  
                        
                            alert (str1+'與'+str2+"衝突"); 
                                      } 
            } 
                     //alert(str1+'與'+str2+"前半部衝突"); 
                var str1XBackPosition = str1.length-str1XPosition-1; 
                var str2XBackPosition = str2.length-str2XPosition-1; 
                var backposition = str1XBackPosition>str2XBackPosition?str2XBackPosition:str1XBackPosition; 
                if (backposition==0) 
             alert(str1+'與'+str2+"衝突"); 
                } 
                                 var patbackStr1 = str1.substring(str1.length-backposition,str1.length); 
                    var patbackStr2 = str2.substring(str2.length-backposition,str2.length); 
                                       alert(str1+'與'+str2+"衝突"); 
                  } 
                } 
        else if((str1XPosition==-1 && str2XPosition!=-1)||(str1XPos ition!=-1 && str2XPosition!=-1))||(str1XPos ition!=-1 && str2XPosition==-1))||//有且只有一個字串內含* 
        { 
            var strX = str1XPosition==-1?str2:str )//包含*的字串==-1?str1:str2;//清楚*的字串 
            if (strX .length-1            { 
                          
                if(position==0) 
               //alert(str1+'與'+str2+"前半部衝突"); 
                    var backposition = strX.length-position-1; 
                                 Alert(str1+'與'+str2+"衝突"); 
                    } 
                  { 
var patbackStr1 = str1.substring(str1.length-backposition,str1.length); 
                        var patbackStr2 = str2.substring(str2.length-backposition,str2.length); 
                       if(checkMarchQ(patbackStr1,patbackStr2)) 
                      { 
警報(str1+'與'+str2+"衝突") ; 
                                      } 
                               var patbeforeStr1 = str1.substring(0,位置); 
          if(checkMarchQ(patbeforeStr1,patbeforeStr2)) 
                   { ert(str1+'與'+str2+"前半部衝突"); 
                                 if (backposition==0) 

                            alert(str       } 
                      ,其他                           var pat backStr1 = str1.substring(str1.)            var patbackStr2 = str2.substring(str2 .length-backposition,str2.length); 
                                         { 
                                             } 

                    } 
                } 
            } 
        } 
        else 
        { 
            if(checkMarchQ(str1,str2)) 
            { 
                alert(str1+'與'+str2+"衝突"); 
            } 
        } 
    } 
    function checkMarchQ(str1 ,str2) 
    { 
        var flagque = false; 
                //長度相同才有可能衝突 
            for (var i=0 ;i            { 
                if(str.)        { 
                   if(str1.substr(i,1)!=str2. substr(i,1)) 
                    { 
                        flagque = false;//表示不衝突 
                        break; 
                    } 
                    else 
                    { 
                        flagque = true;//表示衝突 
                    } 
                } 
            } 
        } 
        return flagque; 


其中*最起碼為一個字符,以上程序並沒有對輸入的合法性進行驗證

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn