首页 > web前端 > js教程 > 正文

JavaScript中两个字符串的匹配

高洛峰
发布: 2016-11-26 14:42:17
原创
1215 人浏览过

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

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

要求可以提示出两个冲突






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

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


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板