验证下面三种类型的链接
http://localhost:11594/A6P5/index.html
http://localhost:11594/P5/index.html 
http://localhost:11594/index.html A[可以是A或T这二个字母]\d*
P[就只有一个字母]\d*
验证是{A6P5}还是{P5}类型的链接
并取得字母后面的数字

解决方案 »

  1.   


    验证 含有 A6P5 的连接 并取得数字 6和5

    验证 含有 P5 的连接 并取得数字 5是这样吗?
      

  2.   

    http://localhost:11594/([A|T](\d{1,1})P(\d{1,1})|P(\d{1,1}))/index.html 
      

  3.   


    s = "http://localhost:11594/A6P5/index.html" 
    s = "http://localhost:11594/P5/index.html" 
    //s = "http://localhost:11594/index.html"var re = new RegExp("\/[AT](\\d+)P(\\d+)\/","");
    if(re.test(s)){
       alert("类型1");
       var a = s.match(re);
       alert(a[1] + "," + a[2])
    }
    else{
        var re = new RegExp("\/P(\\d+)\/","");
        if(re.test(s)){
            alert("类型2")
            var a = s.match(re);
            alert(a[1])
       }
        else alert("类型3")
    }
      

  4.   

    刚测试通过了... :)
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    </head>
    <script language="javascript" type="text/javascript">
    function doCHK()
    {
    var url;
    //url="http://localhost:11594/A6P5/index.html";
    //url="http://localhost:11594/P5/index.html ";
    //url="http://localhost:11594/index.html ";
    //var url="http://localhost:11594/P5/a3/index.html ";
    url=document.getElementById("content1").value;
    var reg1=new RegExp("((http:\/\/localhost:11594\/)(A6P5)(\/index.html))","ig");
    var reg2=new RegExp("((http:\/\/localhost:11594\/)(P5)(\/index.html))","ig");
    if (reg1.test(url)||reg2.test(url))
    {
    reg1.exec(url);
    reg2.exec(url);
    if ((RegExp.$3).length<4)
    alert(RegExp.$1+"\n\n第一个数字:"+(RegExp.$3).substring(1,2));
    else
    alert(RegExp.$1+"\n\n第一个数字:"+(RegExp.$3).substring(1,2)+"\n第二个数字:"+(RegExp.$3).substring(3,4))
    }else{
    alert("不匹配");
    }
    }
    </script>
    <body>
    <p>
    正则匹配问题<br>
    <input name="content1" type="text" id="content1" value="http://localhost:11594/A6P5/index.html" size="60">
    <input name="BTN01" value="Test" type="button" onClick="javascript:doCHK();">
    </p>
    </body>
    </html>