如何知道url路径是否包含一些字符串

时间:2015-06-03 04:32:54

标签: javascript

我可能有以下网址:

localhost/mysite
localhost/mysite/index.php
localhost/mysite/#hashstring
localhost/mysite/index.php/#hasstring
localhost/mystie/index.php/path
localhost/mysite/index.php/path#hashstring
localhost/mystie/index.php/someotherpath

所以在上面的网址中我想检查一下index.php之后是否有一些路径字符串。因此/index.php/path, /index.php/path#hashstring, and index.php/someotherpath返回true ,但其他网址应返回false。

1 个答案:

答案 0 :(得分:-1)

尝试这种方式:

function check(){
 var str = "localhost/mysite/index.php/#hasstring";
 var flag = false;
 var idx = str.indexOf("index.php/");]
 var length = str.length; 
 if( idx >= 0 && length > idx){
   flag = true
 }
 return flag

}