HTML: Markup language
CSS: Styling language
JavaScript: Scripting language
Web APIs: Programming interfaces
All web technology
Learn web development
Discover our tools
Get to know MDN better
このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。
View in English Always switch to English
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 を返します。
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"
includes(searchString) includes(searchString, position)
searchString
str の中で検索される文字の集合です。正規表現にすることはできません。正規表現ではない値はすべて文字列に変換されますので、省略したり undefined を渡したりすると、includes() は "undefined" という文字列を検索します。これはおそらく望むところではないでしょう。
str
undefined
"undefined"
position
文字列内で searchString を検索し始める位置です。(既定値は 0 です。)
0
検索文字列が指定された文字列の中で見つかった場合、searchString が空文字列の場合は true。そうでなければ false です。
TypeError
searchString が正規表現であった場合に発生します。
このメソッドで、ある文字列が別な文字列の中に含まれているかどうかを判断することができます。
includes() メソッドは大文字と小文字が区別します。例えば、次のコードでは false を返します。
"Blue Whale".includes("blue"); // false を返す
元の文字列と検索文字列の両方をすべて小文字に変換することで、この制約を回避することができます。
"Blue Whale".toLowerCase().includes("blue"); // true を返す
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
Enable JavaScript to view this browser compatibility table.
String.prototype.includes
core-js
Array.prototype.includes()
TypedArray.prototype.includes()
String.prototype.indexOf()
String.prototype.lastIndexOf()
String.prototype.startsWith()
String.prototype.endsWith()