location.href只能得到http://dby.csdn.net/dby/xxx.asp,我只要取到http://dby.csdn.net即可,这么从字符串中得到我想要的字符串呢?谢谢!

解决方案 »

  1.   

    <script>
    function geturl()
    {
       var url=/(\w+:\/\/[\w.]+)\/(\S*)/;
       //var iehref=window.location.href;
       iehref="http://dby.csdn.net/dby/xxx.asp";
       var result=iehref.match(url);
       if(result!=null) {
          return result[1];
       }
    }
    </script>
    <input type=button onclick="alert(geturl())">
      

  2.   

    <script>
    function geturl()
    {
       var url=/(\w+:\/\/[\w.]+)\/(\S*)/;
       var newweb="http://www.sohu.com/";  //新的网址。
       //var iehref=window.location.href;  
       iehref="http://dby.csdn.net/dby/xxx.asp";
       var result=iehref.match(url);
       if(result!=null) {
          return newweb+result[2]; 
       }
    }
    </script>
    <input type=button onclick="alert(geturl())">