if((location.href).indexOf("index")!=-1)alert("含有index")

解决方案 »

  1.   

    function check(str)  
    {
      var file = str.match(/\/([^\?/]*)(\?|$)/)[1];
      return file.split(".")[0].indexOf("index")>-1;
    }
    alert(check("http://127.0.0.1/index.htm"));
      

  2.   


    alert(/index/.test(location.href))
      

  3.   

    To: shyslysky(飞天) 假如 href = http://127.0.0.1/index/abc.htm ??
      

  4.   

    页面名称含有index:
    var str=location.href
    alert(/index/.test(str.substring(str.lastIndexOf("\/"),str.length)))
      

  5.   

    to: meizz(梅花雪)
    恩,我开始理解错了,不过老大你别搞那么复杂的正则阿,看晕了:D
      

  6.   

    To meizz(梅花雪)如果想判断多个return file.split(".")[0].indexOf("index","indexmain")>-1;这样写对吗
      

  7.   

    再 To shyslysky(飞天)  href = http://127.0.0.1/index/abc.asp?abc=indexhaha
      

  8.   

    To meizz(梅花雪) indexOf()内的字符串对大小有关吗
      

  9.   

    To: wchunge(山子) 判断多个:
    function check(str)  
    {
      var file = str.match(/\/([^\?/]*)(\?|$)/)[1];
      return /index|indexmain/i.test(file.split(".")[0]);
    }
    alert(check("http://127.0.0.1/index.htm"));
    alert(check("http://127.0.0.1/indeXmain.htm"));
      

  10.   

    var str="http://23index5/.jsp?indexa"
    alert(/index/.test(str.substring(str.lastIndexOf("\/"),str.indexOf("?")==-1?str.length:(str.indexOf("?")-1))))
      

  11.   

    To meizz(梅花雪)
    再提要求,我的复杂度就要高过你了:(
      

  12.   

    To:  shyslysky(飞天) 我都快有点不忍心了, 不过想了想我还是要说的:href = http://23index5/aa.jsp?indexURL=../web/index.jsp?id=1
      

  13.   

    To: 楼主, indexOf() 是区分大小写的, 即 index 与 Index 得到的结果是不同的
      

  14.   

    To meizz(梅花雪) xx.asp,bb.asp写成js包含文件引用,用您写的函数判断当前是哪个页面,然后改变相应的文字,如颜色等;但我用您的函数,不知道当前的文件名是什么
    url = location.href
    check(url)
    alert都是真,让我不知道如何做,请帮忙.谢谢
      

  15.   

    function check(str)  
    {
      var file = str.match(/\/([^\?/]*)(\?|$)/)[1];
      alert("filename = "+ file); //在这里你可以把网页名弹出来看看取的对不对
      return /index|indexmain/i.test(file.split(".")[0]);
    }
    alert(check(window.location.href));
    //alert(check("http://23index5/aa.jsp?indexURL=../web/index.jsp?id=1"));我测试没有问题的呀! IE6