怎么样用JS分隔出我想要的字符,我想把下面的http://www.xinquanxun.com/?123456&dfdfd中的123456提取出来。
我用了indexOf和subString,但不灵活,请问各位有没更好的办法呢?

解决方案 »

  1.   

    js用律例子:使用split分隔指定字符串JavaScript使用split分隔指定字符串,用法举例,这里使用“,”逗号作为分隔符,将一长串含有逗号的字符串分隔,应用十分普遍,其用法也相当简单,你参考下下面的可学到用法。
    <script language="javascript">
    <!--
    name = "张三,李四,王五";
    ch = new Array;
    ch = name.split(",");
    for(i=0;i<ch.length;i++){
    document.write(ch[i],"<br>");
    }
    //-->
    </script>
      

  2.   


    str="http://www.xinquanxun.com/?123456&dfdfd";newstr=str.split("?")[1].split("&")[0];
    alert(newstr);
      

  3.   

    str="http://www.xinquanxun.com/?123456&dfdfd";newstr=str.match(/\?(.+?)&/);
    alert(newstr[1]);