例如有数字2001  2002  2003 2004 3001 3002 3003 3004 4201 4202 4203 4204 要转变为:200[1-4]|300[1-4]|420[1-4]该怎么做?

解决方案 »

  1.   

    正则不是只有一种写法
    var a = [2001 ,2002, 2003 ,2004 ,3001 ,3002 ,3003 ,3004 ,4201 , 4202, 4203,4204];
    var reg = new RegExp('^' + a.join('|') + '$');
      

  2.   

    我写了几个代码,你看看吧................
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <script type="text/javascript">
       function loadend(){
       var req = "";
       var tempReq = "";
       var flag = false;
    var array = [2001,2002,2003,2004,3001,3002,3003,3004,4201,4202,4203,4204,50,51,69,70];
    for(i = 1;i < array.length;i ++){
    //如果连续
    if(array[i]-array[i-1] == 1){
    //加那个“|“符号
    if(req != "" && tempReq == "") tempReq = "|";
    //如果十位上的数相同
    if(Math.floor(array[i]/10) == Math.floor(array[i-1]/10)){
    if(tempReq == "" || tempReq == "|"){
    tempReq += Math.floor(array[i]/10) + "[" + array[i-1]%10;
    flag = true;
    }
    tempReq += array[i]%10;
    }else{
    //4楼的问题我就没考虑了...
    }
    }else{
    tempReq += "]";
    req += tempReq;
    tempReq = "";
    flag = false;
    }
    }
    if(flag){
    tempReq += "]";
    req += tempReq;
    }
    document.write(req);
       }
      </script>
        <title>MyHtml.html</title>

        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="this is my page">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        
        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  </head>
      
      <body onload="loadend();">
      </body>
    </html>