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
廣告