Widely available すべての主要ブラウザで対応済み。安心して使用可能。

概要

String.prototype.includes() は文字列に指定した部分文字列が含まれるかを真偽値で返します。indexOf !== -1 より可読性が高く、startsWith/endsWith と合わせて直感的な文字列判定が可能です。

対応ブラウザ

機能 デスクトップ モバイル
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
41
12
40
9
41
9
1+対応 (バージョン) 未対応 注釈あり サブ機能の解説は MDN Web Docs (CC BY-SA 2.5)
注釈 2件
削除済み
  • このバージョンで機能が削除されました (48)
対応条件
  • 以前は別名で対応していました: contains (18)

基本構文

JAVASCRIPT
const url = 'https://example.com/api/users';

url.includes('api');       // true
url.startsWith('https');   // true
url.endsWith('/users');    // true

// Specifying the search start position
'hello world'.includes('world', 6); // true

ライブデモ

ki-wa-dsearch

specific. ki-wa-d that include or check..

プレビュー全画面表示

forwardmatch and backwardmatch

StartsWith and endsWith usingcheck..

プレビュー全画面表示

SearchStartposition. specified

2argument in search start specified in..

プレビュー全画面表示

実務での使いどころ

  • String includes() の活用

    文字列に特定の部分文字列が含まれるかを判定するメソッド。startsWith, endsWith も同時に追加された。

注意点

  • 特になし。すべての主要ブラウザで安定して動作する。

アクセシビリティ

  • JavaScript による動的更新時は、aria-live リージョンで変更をスクリーンリーダーに通知する。

Powered by web-features