javascript自定義startWith()和endWith()的兩種方法
更新時間:2013年11月11日 17:12:23 作者:
js中自定義startWith()和endWith()方法有兩種,在本文將為大家詳細(xì)介紹下,感興趣的朋友不要錯過
一、采用正則表達式實現(xiàn)startWith、endWith效果函數(shù)
String.prototype.startWith=function(str){
var reg=new RegExp("^"+str);
return reg.test(this);
}
//測試ok,直接使用str.endWith("abc")方式調(diào)用即可
String.prototype.endWith=function(str){
var reg=new RegExp(str+"$");
return reg.test(this);
}
二、JavaScript實現(xiàn)startWith、endWith效果函數(shù)
<script type="text/javascript">
String.prototype.endWith=function(s){
if(s==null||s==""||this.length==0||s.length>this.length)
return false;
if(this.substring(this.length-s.length)==s)
return true;
else
return false;
return true;
}
String.prototype.startWith=function(s){
if(s==null||s==""||this.length==0||s.length>this.length)
return false;
if(this.substr(0,s.length)==s)
return true;
else
return false;
return true;
}
</script>
//以下是使用示例
var url = location.href;
if (url.startWith('http://www.dhdzp.com'))
{
//如果當(dāng)前url是以 http://www.dhdzp.com/ 開頭
}
復(fù)制代碼 代碼如下:
String.prototype.startWith=function(str){
var reg=new RegExp("^"+str);
return reg.test(this);
}
//測試ok,直接使用str.endWith("abc")方式調(diào)用即可
String.prototype.endWith=function(str){
var reg=new RegExp(str+"$");
return reg.test(this);
}
二、JavaScript實現(xiàn)startWith、endWith效果函數(shù)
復(fù)制代碼 代碼如下:
<script type="text/javascript">
String.prototype.endWith=function(s){
if(s==null||s==""||this.length==0||s.length>this.length)
return false;
if(this.substring(this.length-s.length)==s)
return true;
else
return false;
return true;
}
String.prototype.startWith=function(s){
if(s==null||s==""||this.length==0||s.length>this.length)
return false;
if(this.substr(0,s.length)==s)
return true;
else
return false;
return true;
}
</script>
//以下是使用示例
var url = location.href;
if (url.startWith('http://www.dhdzp.com'))
{
//如果當(dāng)前url是以 http://www.dhdzp.com/ 開頭
}
相關(guān)文章
Firefox下提示illegal character并出現(xiàn)亂碼的原因
Firefox下提示illegal character并出現(xiàn)亂碼的問題,時間是是因為編碼的問題導(dǎo)致。2010-03-03
javascript中IE瀏覽器不支持NEW DATE()帶參數(shù)的解決方法
在火狐下 可以正常取得時間,在IE7下 卻是 NaN。糾結(jié)老長時間,放棄了new date 然后再老外的論壇中找了一段段代碼可以兼容所有瀏覽器的格式化日期代碼2012-03-03
ionic+html5+API實現(xiàn)雙擊返回鍵退出應(yīng)用
這篇文章主要為大家詳細(xì)介紹了ionic+html5+API實現(xiàn)雙擊返回鍵退出應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09
(跨瀏覽器基礎(chǔ)事件/瀏覽器檢測/判斷瀏覽器)經(jīng)驗代碼分享
一些js代碼,自己備用的,高手不要笑話我。(跨瀏覽器基礎(chǔ)事件,瀏覽器檢測,判斷瀏覽器的名稱、版本號、操作系統(tǒng))等等,很實用的,方便自己使用,感興趣的朋友可以了解下,希望本文對你有所幫助2013-01-01

