JavaScript 字符串 includes() 方法



JavaScript 字符串includes()方法搜索字符串的一部分,并确定指定字符串是否在原始字符串中找到。如果在当前字符串中找到该字符串,则返回布尔值'true',否则返回'false'

这是一个区分大小写的函数,它将字符串“hi”和“Hi”视为两个不同的字符串值。如果搜索字符串是正则表达式(正则表达式),则会抛出“TypeError”异常。

语法

以下是 JavaScript 字符串includes()方法的语法:

includes(searchString, position)

参数

此方法接受两个参数:'searchString' 和 'position',如下所述:

  • searchString - 要搜索的字符串。
  • position - 开始搜索的位置。

返回值

如果在原始字符串中找到指定的字符串,则此方法返回'true';否则返回'false'。

示例 1

如果在原始字符串中找到 searchString,则此方法将返回'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

如果我们将searchStringposition参数都传递给此方法,它将从指定位置开始搜索searchString。

以下是 JavaScript 字符串includes()方法的另一个示例。在此示例中,我们在字符串“Hypertext Markup Language”中从起始位置12搜索搜索字符串'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 是正则表达式(正则表达式),则此方法将抛出'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
广告