String.prototype.includes()
String
值的 includes()
方法执行区分大小写的搜索,以确定是否可以在该字符串中找到给定字符串,并根据需要返回 true
或 false
。
¥The includes()
method of String
values performs a case-sensitive search to determine whether a given string may be found within this string, returning true
or false
as appropriate.
Try it
语法
参数
¥Parameters
searchString
-
要在
str
内搜索的字符串。不能 是一个正则表达式。所有非正则表达式的值都是 强制为字符串,因此省略它或传递undefined
会导致includes()
搜索字符串"undefined"
,这很少是你想要的。 position
Optional-
字符串中开始搜索
searchString
的位置。(默认为0
。)
返回值
例外情况
描述
区分大小写
¥Case-sensitivity
includes()
方法区分大小写。例如,以下表达式返回 false
:
¥The includes()
method is case sensitive. For example, the following expression returns false
:
"Blue Whale".includes("blue"); // returns false
你可以通过将原始字符串和搜索字符串全部转换为小写来解决此约束:
¥You can work around this constraint by transforming both the original string and the search string to all lowercase:
"Blue Whale".toLowerCase().includes("blue"); // returns true
示例
使用包含()
¥Using includes()
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