示例1:http://news.jinti.com/news/20090901/251947.htm
示例2:http://news.jinti.com/news/20098201/247.htm
示例3:http://news.jinti.com/news/20101001/3219723.htm
如何用js正则取流水号?结果应该是:2519472473219723

解决方案 »

  1.   


    var str = "http://news.jinti.com/news/20090901/251947.htm"
    var no = str.match(/(\d+)\.htm/i);alert(no[1]);
      

  2.   


    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
            <title></title>
            <script language="JavaScript" type="text/javascript">
                window.onload=function(){
                    var s1="http://news.jinti.com/news/20090901/251947.htm ";
                    var s2="http://news.jinti.com/news/20098201/247.htm" ;
                    var s3="http://news.jinti.com/news/20101001/3219723.htm ";
                    var rex=/(\d+).htm/g;
                    if(rex.test(s1)) alert(RegExp.$1);
                }
            </script>
        </head>
        <body>
        </body>
    </html>
      

  3.   

    上面都是正确的,不过我还是再来个名字比较酷的“零宽度正预测先行断言”
    str.match(/\d+(?=\.htm)/)[0];