String includes()
String.prototype.includes() は文字列に指定した部分文字列が含まれるかを真偽値で返します。indexOf !== -1 より可読性が高く、startsWith/endsWith と合わせて直感的な文字列判定が可能です。
概要
String.prototype.includes() は文字列に指定した部分文字列が含まれるかを真偽値で返します。indexOf !== -1 より可読性が高く、startsWith/endsWith と合わせて直感的な文字列判定が可能です。
対応ブラウザ
| 機能 | デスクトップ | モバイル | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 41 | 12 | 40 | 9 | 41 | 9 | |
注釈 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 ライブデモ
実務での使いどころ
-
String includes() の活用
文字列に特定の部分文字列が含まれるかを判定するメソッド。startsWith, endsWith も同時に追加された。
注意点
- 特になし。すべての主要ブラウザで安定して動作する。
アクセシビリティ
- JavaScript による動的更新時は、aria-live リージョンで変更をスクリーンリーダーに通知する。
参考リンク
Powered by web-features