JavaScript 字符串 includes() 方法
JavaScript 字符串的 includes() 方法搜索字符串的一部分,并判断指定的字符串是否在原始字符串中找到。如果在当前字符串中找到该字符串,则返回布尔值 'true';否则,返回 'false'。
此方法区分大小写,将字符串"hi"和"Hi"视为两个不同的字符串值。它会抛出"TypeError"异常;如果搜索字符串是正则表达式 (regex)。
语法
以下是 JavaScript 字符串 includes() 方法的语法 -
includes(searchString,position)
参数
此方法接受两个参数:"searchString"和"position",如下所述 -
- searchString - 要搜索的字符串。
- position - 搜索开始的位置。
返回值
如果在原始字符串中找到指定的字符串,则此方法返回"true";否则返回 'false'。
示例 1
如果在原始字符串中找到了搜索字符串,则此方法将返回 'true'。
在下面的示例中,我们使用 String JavaScript includes() 方法来确定搜索字符串"Tutorials Point"是否存在于原始字符串"Welcome to Tutorials Point"中。
<html>
<head>
<title>JavaScript String includes() Method</title>
</head>
<body>
<script>
const str = "Welcome to Tutorials Point";
const searchString = "Tutorials Point";
document.write("Original string: ", str);
document.write("<br>Search string: ", searchString);
document.write("<br>Is string '", searchString,"' found in '", str, "' or not? ", str.includes(searchString));
</script>
</body>
</html>
输出
上述程序返回"true"。
Original string: Welcome to Tutorials Point Search string: Tutorials Point Is string 'Tutorials Point' found in 'Welcome to Tutorials Point' or not? true
示例 2
如果我们同时将 searchString 和 position 参数传递给此方法,它将从指定位置开始搜索 searchString。
以下是 JavaScript 字符串 includes() 方法的另一个示例。在此示例中,我们在字符串"超文本标记语言"中的起始位置 12 搜索 searchString"text"。
<html>
<head>
<title>JavaScript String includes() Method</title>
</head>
<body>
<script>
const str = "Hypertext Markup Language";
const searchString = "text";
let position = 12;
document.write("Original string: ", str);
document.write("<br>Search string: ", searchString);
document.write("<br>Position: ", position);
document.write("<br>Is string '", searchString,"' found in '", str, "' or not? ", str.includes(searchString, position));
</script>
</body>
</html>
输出
执行上述程序后,返回"false"。
Original string: Hypertext Markup Language Search string: text Position: 12 Is string 'text' found in 'Hypertext Markup Language' or not? false
示例 3
正如我们之前讨论过的,这是一个区分大小写的方法,因此它会将"hi"和"Hi"视为两个不同的值。例如:"hi how are you".includes("Hi") 方法返回 'false'。
<html>
<head>
<title>JavaScript String includes() Method</title>
</head>
<body>
<script>
const str = "hi how are you";
const searchString = "Hi";
document.write("Original string: ", str);
document.write("<br>Search string: ", searchString);
document.write("<br>Is string '", searchString,"' found in '", str, "' or not? ", str.includes(searchString));
</script>
</body>
</html>
输出
上述程序返回"false"。
Original string: hi how are you Search string: Hi Is string 'Hi' found in 'hi how are you' or not? false
示例 4
如果 searchString 是 regex(正则表达式),此方法将抛出 'TypeError' 异常。
<html>
<head>
<title>JavaScript String includes() Method</title>
</head>
<body>
<script>
const str = "hi how are you";
const searchString = /\w+/;
document.write("Original string: ", str);
document.write("<br>Search string: ", searchString);
try {
document.write(str.includes(searchString));
} catch (error) {
document.write("<br>", error);
}
</script>
</body>
</html>
输出
上述程序执行后,将抛出"TypeError"异常。
Original string: hi how are you Search string: /\w+/ TypeError: First argument to String.prototype.includes must not be a regular expression

