天天看點

js判斷字元串是否包含http [email protected]

1:正則

var str=new RegExp("http");
str.test("httpadfsd");//test方法傳回值為(true或者false)
           

2:indexOf();

var newStr=str.indexOf("http");

if(newStr==0){
   console.log("字元串是以http開頭的!")
}
if(newStr==-1){
  console.log("字元串不是以http開頭的!")
}
           

繼續閱讀