字符串前後判斷踩坑,理解越直白的越不會出錯

判斷一個字符串是否以指定的字符串開始 (未完)

String.prototype.startWith = function (startString) {
    return typeof startString === 'string' && new RegExp('^' + startString).test(this);
    // if(startString && this.length >= startString.length) {
    //     return this.slice(0, startString.length) == startString;
    // }
    // return false;
};
String.prototype.endWith = function (endString) {
    return new RegExp(endString+'$').test(this);
    if (endString && this.length >= endString.length) {
        return this.slice(-endString.length) == endString;
    }
    return false;
}


// 注意
new RegExg([])   new RegExg({})等等 會有轉字符串的變動
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章