Rumah  >  Artikel  >  hujung hadapan web  >  JavaScript中两个字符串的匹配

JavaScript中两个字符串的匹配

高洛峰
高洛峰asal
2016-11-26 14:42:171206semak imbas

工作中遇到一个问题,两个字符串匹配,要求:每个字符串中最多含有一个*,?可以无限多个

*代表一个任意长度的字符串,而?则代表一个字符

要求可以提示出两个冲突






用Javascript实现代码如下:

[javascript] 
function checkMarchX() 
    { 
        var str1 = document.getElementById('str1').value; 
        var str2 = document.getElementById('str2').value; 
        var str1XPosition = str1.indexOf('*'); 
        var str2XPosition = str2.indexOf('*'); 
        if(str1XPosition!=-1 && str2XPosition!=-1)//两者都含有* 
        { 
            var position = str1XPosition>str2XPosition?str2XPosition:str1XPosition;//success 
            if(position!=0) 
            { 
                var patbeforeStr1 = str1.substring(0,position); 
                var patbeforeStr2 = str2.substring(0,position); 
                if(checkMarchQ(patbeforeStr1,patbeforeStr2)) 
                { 
                    //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+"冲突"); 
                    } 
                    else 
                    { 
                        var patbackStr1 = str1.substring(str1.length-backposition,str1.length); 
                        var patbackStr2 = str2.substring(str2.length-backposition,str2.length); 
                        if(checkMarchQ(patbackStr1,patbackStr2)) 
                        { 
                            alert(str1+'与'+str2+"冲突"); 
                        } 
                    } 
                } 
            } 
            else 
            { 
                //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+"冲突"); 
                } 
                else 
                { 
                    var patbackStr1 = str1.substring(str1.length-backposition,str1.length); 
                    var patbackStr2 = str2.substring(str2.length-backposition,str2.length); 
                    if(checkMarchQ(patbackStr1,patbackStr2)) 
                    { 
                        alert(str1+'与'+str2+"冲突"); 
                    } 
                } 
            } 
        } 
        else if((str1XPosition==-1 && str2XPosition!=-1)||(str1XPosition!=-1 && str2XPosition==-1))//有且只有一个字符串含有* 
        { 
            var strX = str1XPosition==-1?str2:str1;//含有*的字符串 
            var strNoX = str1XPosition==-1?str1:str2;//不含*的字符串 
            if (strX.length-1            { 
                var position = strX.indexOf('*'); 
                if(position==0) 
                { 
                    //alert(str1+'与'+str2+"前半部分冲突"); 
                    var backposition = strX.length-position-1; 
                    if (backposition==0) 
                    { 
                        alert(str1+'与'+str2+"冲突"); 
                    } 
                    else 
                    { 
                        var patbackStr1 = str1.substring(str1.length-backposition,str1.length); 
                        var patbackStr2 = str2.substring(str2.length-backposition,str2.length); 
                        if(checkMarchQ(patbackStr1,patbackStr2)) 
                        { 
                            alert(str1+'与'+str2+"冲突"); 
                        } 
                    } 
                } 
                else 
                { 
                    var patbeforeStr1 = str1.substring(0,position); 
                    var patbeforeStr2 = str2.substring(0,position); 
                    if(checkMarchQ(patbeforeStr1,patbeforeStr2)) 
                    { 
                        //alert(str1+'与'+str2+"前半部分冲突"); 
                        var backposition = strX.length-position-1; 
                        if (backposition==0) 
                        { 
                            alert(str1+'与'+str2+"冲突"); 
                        } 
                        else 
                        { 
                            var patbackStr1 = str1.substring(str1.length-backposition,str1.length); 
                            var patbackStr2 = str2.substring(str2.length-backposition,str2.length); 
                            if(checkMarchQ(patbackStr1,patbackStr2)) 
                            { 
                                alert(str1+'与'+str2+"冲突"); 
                            } 
                        } 
                    } 
                } 
            } 
        } 
        else 
        { 
            if(checkMarchQ(str1,str2)) 
            { 
                alert(str1+'与'+str2+"冲突"); 
            } 
        } 
    } 
    function checkMarchQ(str1,str2) 
    { 
        var flagque = false; 
        if(str1.length==str2.length) 
        { 
            //长度相同才有可能冲突 
            for (var i=0 ;i            { 
                if(str1.substr(i,1)!='?' && str2.substr(i,1)!='?') 
                { 
                    if(str1.substr(i,1)!=str2.substr(i,1)) 
                    { 
                        flagque = false;//表示不冲突 
                        break; 
                    } 
                    else 
                    { 
                        flagque = true;//表示冲突 
                    } 
                } 
            } 
        } 
        return flagque; 
    } 

其中*最起码为一个字符,以上程序并没有对输入的合法性进行验证


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:JavaScript实现多维数组Artikel seterusnya:JavaScript构造函数解析