<pre>
//判斷當前字符串是否以str開始
//先判斷是否存在function是避免和js原生方法沖突,自定義方法的效率不如原生的高
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (str){
return this.slice(0, str.length) == str;
};
}
//判斷當前字符串是否以str結(jié)束
if (typeof String.prototype.endsWith != 'function') {
String.prototype.endsWith = function (str){
return this.slice(-str.length) == str;
};
}
</pre>
<small>
轉(zhuǎn)自:javascript中判斷字符串是否以指定字符串開始或結(jié)尾
</small>