Movatterモバイル変換


[0]ホーム

URL:


  1. 開発者向けのウェブ技術
  2. JavaScript
  3. JavaScript リファレンス
  4. 標準組み込みオブジェクト
  5. String
  6. includes()

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。

View in EnglishAlways switch to English

String.prototype.includes()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年9月⁩.

includes()String 値のメソッドで、大文字と小文字を区別して検索を行い、指定された文字列がこの文字列内で見つかるかどうかを判断し、必要に応じてtrue またはfalse を返します。

試してみましょう

const sentence = "The quick brown fox jumps over the lazy dog.";const word = "fox";console.log(  `The word "${word}" ${    sentence.includes(word) ? "is" : "is not"  } in the sentence`,);// 予想される結果: "The word "fox" is in the sentence"

構文

js
includes(searchString)includes(searchString, position)

引数

searchString

str の中で検索される文字の集合です。正規表現にすることはできません。正規表現ではない値はすべて文字列に変換されますので、省略したりundefined を渡したりすると、includes()"undefined" という文字列を検索します。これはおそらく望むところではないでしょう。

position省略可

文字列内でsearchString を検索し始める位置です。(既定値は0 です。)

返値

検索文字列が指定された文字列の中で見つかった場合、searchString が空文字列の場合はtrue。そうでなければfalse です。

例外

TypeError

searchString正規表現であった場合に発生します。

解説

このメソッドで、ある文字列が別な文字列の中に含まれているかどうかを判断することができます。

大文字小文字の区別

includes() メソッドは大文字と小文字が区別します。例えば、次のコードではfalse を返します。

js
"Blue Whale".includes("blue"); // false を返す

元の文字列と検索文字列の両方をすべて小文字に変換することで、この制約を回避することができます。

js
"Blue Whale".toLowerCase().includes("blue"); // true を返す

includes() の使用

js
const str = "To be, or not to be, that is the question.";console.log(str.includes("To be")); // trueconsole.log(str.includes("question")); // trueconsole.log(str.includes("nonexistent")); // falseconsole.log(str.includes("To be", 1)); // falseconsole.log(str.includes("TO BE")); // falseconsole.log(str.includes("")); // true

仕様書

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.includes

ブラウザーの互換性

関連情報

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp