你这个时间类型是字符串格式的么?replace 不可以么?

解决方案 »

  1.   

    正则定义两种方法:var reg=/\d+/g或者var reg=new RegExp("ain",g);你上边的mon定义成这样"/"+iMonth+"月"+"/g"; 之后又 用new定义 ??
      

  2.   


    <script>
    var str="2008年03月28日";
    var i =0;
    while(str.indexOf("年")>-1){str = str.replace('年','.');}
    while(str.indexOf("月")>-1){str = str.replace('月','.');}
    while(str.indexOf("日")>-1){str = str.replace('日','');}
    alert(str);
    </script>
    <body>
    </body>
      

  3.   


    for(iMonth=1;iMonth <=12;iMonth++){ 
    var mon=/^(\d{4})[^\d]+(\d{2})[^\d]+(\d{2}).*$/g
    strLog = strLog.replace(mon,"$1.$2.$3"); 
    }
      

  4.   

    var mon = eval("/"+iMonth+"月"+"/g"); 
    strLog = strLog.replace(mon,iMonth); 
      

  5.   

    <script type="text/javascript">
    var str = "有个时间为 2008年03月14日\n如何转换为 2008.03.14\nfor(iMonth=1;iMonth <=12;iMonth++){\nmon = \"/\"+iMonth+\"月\"+\"/g\";\nstrLog = strLog.replace(new RegExp(mon,\"g\"),iMonth);\n";
    var resu;
    resu = str.replace(/([0-9]{2,4})年([0-9]{1,2})月([0-9]{1,2})日/g,"$1.$2.$3");
    alert(resu);
    </script>