includes用法:
str.includes(searchString , [position])
searchString
在此字符串內(nèi)搜索的字符串公条。
position
可選的字符串中開始搜索的位置searchString
。(默認(rèn)為0)。
返回值:
true:
如果搜索字符串在給定字符串內(nèi)的任何地方找到;返回true
false:
如果沒有找到返回false
描述:
此方法可讓您確定一個字符串是否包含另一個字符串。
該includes()
方法區(qū)分大小寫。例如唯卖,以下表達(dá)式返回false:
'Blue Whale'.includes('blue'); // returns false
運用:
var str = 'To be, or not to be, that is the question.';
console.log(str.includes('To be')); // true
console.log(str.includes('question')); // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1)); // false
console.log(str.includes('TO BE')); // false
填充工具:
此方法已添加到ECMAScript 2015規(guī)范中,可能尚未在所有JavaScript實現(xiàn)中提供躬柬。但是拜轨,您可以輕松地填充此方法:
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) { 'use strict'; if (typeof start !== 'number') {
start = 0;
} if (start + search.length > this.length) { return false;
} else { return this.indexOf(search, start) !== -1;
}
};
}
補充es5
es5中是用indexOf的命令來查找的,存在的返回的是索引值允青,不存在返回-1橄碾,但是NaN查找不出來,因為NaN!==NaN
經(jīng)典前端面試題每日更新颠锉,歡迎參與討論法牲,地址:https://github.com/daily-interview/fe-interview。
更多angular1/2/4/5琼掠、ionic1/2/3拒垃、react、vue瓷蛙、微信小程序悼瓮、nodejs等技術(shù)文章、視頻教程和開源項目艰猬,請關(guān)注微信公眾號——全棧弄潮兒横堡。
image
腦筋急轉(zhuǎn)彎:
image
生活小竅門
image