String.prototype.includes()
includes() 方法执行区分大小写的搜索,以确定是否可以在另一个字符串中找到一个字符串,并根据情况返回 true 或 false。
尝试一下
语法
js
includes(searchString)
includes(searchString, position)
参数
searchString- 
    
要在
str中搜索的字符串。不能是正则表达式。 position可选- 
    
在字符串中开始搜索
searchString的位置。(默认为0。) 
返回值
如果当前字符串包含被搜寻的字符串,就返回 true,否则返回 false。
异常
描述
此方法可以帮你判断一个字符串是否包含另外一个字符串。
区分大小写
includes() 方法是区分大小写的。例如,下面的表达式会返回 false:
js
"Blue Whale".includes("blue"); // returns false
你可以通过将原字符串和搜索字符串全部转换为小写来解决这个约束:
js
"Blue Whale".toLowerCase().includes("blue"); // returns true
示例
使用 includes()
js
const 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
console.log(str.includes("")); // true
规范
| Specification | 
|---|
| ECMAScript Language Specification  # sec-string.prototype.includes  | 
浏览器兼容性
BCD tables only load in the browser