Maison >interface Web >js tutoriel > JavaScript中两个字符串的匹配

JavaScript中两个字符串的匹配

高洛峰
高洛峰original
2016-11-26 14:42:171223parcourir

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

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

要求可以提示出两个冲突






用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; 
    } 

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


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:JavaScript实现多维数组Article suivant:JavaScript构造函数解析