<script type="text/javascript">
var str=new Array("http://localhost/index.html ","http://localhost/name/index.html ","http://localhost/name/111/222/333/444/index.html ")
for(i=0;i<str.length;i++)
alert(str[i]);
</script>其实我是想过滤一些字符,alert结果分别为:
http://localhost
http://localhost/name
http://localhost/name代码该怎么写

解决方案 »

  1.   


      <script type="text/javascript">
      <!--
    var str = ["http://localhost/index.html","http://localhost/name/index.html ","http://localhost/name/111/222/333/444/index.html"];
    alert(str.join(",").match(/http:\/\/localhost(\/name)*/g));
      //-->
      </script>
      

  2.   

    你的意思是不是 如果
    http://localhost 后面直接接/index.html就只需要http://localhost 
    如果http://localhost 与 /index.html之间还有路径 就取http://localhost + 第一个路径比如http://localhost/srcOne/index.html===>http://localhost/srcOne
    http://localhost/srctwo/srcthree/index.html====http://localhost/srctwo希望自己把问题想清楚再提问
      

  3.   

      
    <script language="javascript">
      $(function(){
    var str=new Array("http://localhost/index.html ","http://localhost/name/index.html ","http://localhost/name/111/222/333/444/index.html ");
    $.each(str,function(i){
    alert(str[i].slice(0,str[i].lastIndexOf("/")));
    });
      });
      </script>