indexOf()【順方向 文字列検索】
lastIndexOf()【逆方向 文字列検索】
includes()【部分文字列判定】
search()【正規表現検索 (簡易)】
match()【正規表現検索 (詳細)】
String.prototype.indexOf()【順方向 文字列検索】
メモ
- 文字列を順方向 (前 → 後)で検索し、一致位置を返却
- 逆方向の検索:lastIndexOf()【逆方向 文字列検索】 (一致位置の返却)
- 検索開始位置の指定:includes()【部分文字列判定】 (結果のみ)
- 正規表現検索:
- search()【正規表現検索 (簡易)】 (単一検索)
- match()【正規表現検索 (詳細)】 (複数検索可)
- RegExp【正規表現】オブジェクト のメソッド
- 外部リンク (英語)
String.prototype.indexOf (searchString, position)
String.prototype.indexOf ( searchString [ , position ] )ES2016 (7) ES2015 (6) ES5.1
構文
string.indexOf( searchString, position )
string.indexOf( searchString[, position] )
戻り値一致文字列位置 (詳細は下記参照)
searchString検索文字列
position検索開始位置 (0 ~ 【文字列長】:詳細は下記参照)
| position | 補正値 |
|---|---|
| 省略 | 0 |
| position < 0 | 0 |
| 【文字列長】 < position | 【文字列長】 |
| 戻り値 | 説明 |
|---|---|
| 0 ≦ 戻り値 | 一致あり |
| -1 | 一致なし |
例
var str = "あいうえおあいうえおあいうえお";
console.log(str.indexOf("うえ", 0)); // 出力:2
console.log(str.indexOf("うえ", 5)); // 出力:7
console.log(str.indexOf("うえ", 10)); // 出力:12
console.log(str.indexOf("うえ", 15)); // 出力:-1
console.log(str.indexOf("うえ", -99)); // 出力:2
console.log(str.indexOf("うえ", 99)); // 出力:-1
console.log(str.indexOf("うえ")); // 出力:2
String.prototype.lastIndexOf()【逆方向 文字列検索】
メモ
- 文字列を逆方向 (前 ← 後)で検索し、一致位置を返却
- 順方向の検索:indexOf()【順方向 文字列検索】 (一致位置の返却)
- 検索開始位置の指定:includes()【部分文字列判定】 (結果のみ)
- 正規表現検索:
- search()【正規表現検索 (簡易)】 (単一検索)
- match()【正規表現検索 (詳細)】 (複数検索可)
- RegExp【正規表現】オブジェクト のメソッド
- 外部リンク (英語)
String.prototype.lastIndexOf (searchString, position)
String.prototype.lastIndexOf ( searchString [ , position ] )ES2016 (7) ES2015 (6) ES5.1
構文
string.lastIndexOf( searchString, position )
string.lastIndexOf( searchString[, position] )
戻り値一致文字列位置 (詳細は下記参照)
searchString検索文字列
position検索開始位置 (0 ~:indexOf() と同様 左からの位置 / 詳細は下記参照)
| position | 補正値 |
|---|---|
| 省略 | 【文字列長】 |
| position < 0 | 0 |
| 【文字列長】 < position | 【文字列長】 |
| 戻り値 | 説明 |
|---|---|
| 0 ≦ 戻り値 | 一致あり |
| -1 | 一致なし |
例
var str = "あいうえおあいうえおあいうえお";
console.log(str.lastIndexOf("うえ", 0)); // 出力:-1
console.log(str.lastIndexOf("うえ", 5)); // 出力:2
console.log(str.lastIndexOf("うえ", 10)); // 出力:7
console.log(str.lastIndexOf("うえ", 15)); // 出力:12
console.log(str.lastIndexOf("うえ", -99)); // 出力:-1
console.log(str.lastIndexOf("うえ", 99)); // 出力:12
console.log(str.lastIndexOf("うえ")); // 出力:12
String.prototype.includes()【部分文字列判定】
メモ
- 部分文字列の判定
- 正規表現指定は不可
- 順方向の検索:indexOf()【順方向 文字列検索】 (一致位置の返却)
- 逆方向の検索:lastIndexOf()【逆方向 文字列検索】 (一致位置の返却)
- 正規表現検索:
- search()【正規表現検索 (簡易)】 (単一検索)
- match()【正規表現検索 (詳細)】 (複数検索可)
- RegExp【正規表現】オブジェクト のメソッド
- 関連
- 外部リンク (英語)
String.prototype.includes ( searchString [ , position ] ) ES2016 (7) ES2015 (6) ES5.1
構文
string.includes( searchString[, position] )
戻り値判定結果
true:検索文字列を含む
false:検索文字列を含まない
searchString検索文字列 (正規表現不可)
position検索開始位置 (詳細は下記参照)
例外TypeError 例外 (searchString が正規表現)
(RegExp[ @@match ]【正規表現判定】プロパティ をfalseに設定すれば回避可能)
| position | 説明 |
|---|---|
| 0 ≦ start | 先頭からの文字位置 (0:先頭) 【文字列長】 < start:文字列長 |
| start < 0 | 0 (先頭) |
例
var str = "あいうえお";
console.log(str.includes("いう")); // 出力:true
console.log(str.includes("いう", 1)); // 出力:true
console.log(str.includes("いう", 2)); // 出力:false
var regEx = /い.*え/;
regEx[Symbol.match] = false; // RegExp[@@match]
console.log(str.includes(regEx)); // 出力:false
console.log(str.includes(/い.*え/)); // TypeError 例外
String.prototype.search()【正規表現検索 (簡易)】
メモ
- 簡易な正規表現の検索 (検索結果のみ)
- 内部からRegExp[ @@search ] 【検索 (簡易)】 を呼び出し
- 参照:検索フラグ・正規表現パターン
- 詳細な正規表現の検索:match()【正規表現検索 (詳細)】 (複数検索可)
- その他検索
- indexOf()【順方向 文字列検索】 (一致位置の返却)
- lastIndexOf()【逆方向 文字列検索】 (一致位置の返却)
- includes()【部分文字列判定】 (検索開始位置の指定・結果のみ)
- 外部リンク (英語)
String.prototype.search ( regexp ) ES2016 (7) ES2015 (6) ES5.1
構文
string.search( regexp )
戻り値一致文字列位置 (詳細は下記参照)
regexpRegExp【正規表現】オブジェクト (正規表現リテラル) (それ以外は作成)
| 戻り値 | 説明 |
|---|---|
| 0 ≦ 戻り値 | 一致あり |
| -1 | 一致なし |
例
var str = "あいうえおあいUえおあいuえお";
console.log(str.search(/い.え/)); // 出力:1
console.log(str.search(/きくけ/)); // 出力:-1
console.log(str.search("あいうえお")); // 出力:0
console.log(str.search("かきくけこ")); // 出力:-1
String.prototype.match()【正規表現検索 (詳細)】
メモ
- 詳細な正規表現の検索 (戻り値:検索結果配列)
- 'g'(グローバル検索) フラグ指定で複数検索
- 内部からRegExp[ @@match ] 【検索 (詳細)】を呼び出し
- 参照:検索フラグ・正規表現パターン
- 簡易な正規表現の検索:search()【正規表現検索 (簡易)】
- その他検索:
- indexOf()【順方向 文字列検索】 (一致位置の返却)
- lastIndexOf()【逆方向 文字列検索】 (一致位置の返却)
- includes()【部分文字列判定】 (検索開始位置の指定・結果のみ)
- 外部リンク (英語)
String.prototype.match ( regexp ) ES2016 (7) ES2015 (6) ES5.1
構文
string.match( regexp )
戻り値検索結果配列 (一致なし:null / 詳細は下記参照)
regexpRegExp【正規表現】オブジェクト (正規表現リテラル) (それ以外は作成)
| 'g'(グローバル検索) フラグ | 戻り値の説明 |
|---|---|
| 未指定 | [0]:一致文字列 [1] ~:キャプチャ文字列 (名前付きキャプチャも含む) groups:名前付きキャプチャの連想配列 (キャプチャ名・キャプチャ文字列) index:マッチ位置 (0~) input:入力の被検索文字列 length:配列数 (RegExp【正規表現】オブジェクトの exec()【検索】と同等の処理) |
| 指定 | [0] ~:全てのマッチ文字列 |
例
const str = "WORD word1 word2 word3 word4 word5";
let result = str.match(/[a-z0-9]+/);
console.log(result.length); // 出力:1
console.log(result[0]); // 出力:word1
console.log(result.index); // 出力:5
console.log(result.input); // 出力:WORD word1 word2 word3 word4 word5
result = str.match(/[a-z0-9]+/g);
console.log(result); // 出力:Array(5) ["word1", "word2", "word3", "word4", "word5"]
result = str.match(/[xyz]+/);
console.log(result); // 出力:null
result = str.match("word");
console.log(result.length); // 出力:1
console.log(result[0]); // 出力:word
console.log(result.index); // 出力:5
console.log(result.input); // 出力:WORD word1 word2 word3 word4 word5
result = str.match("xyz");
console.log(result); // 出力:null
const str = "WORD word1 word2 word3 word4 word5";
let result = str.match(/([a-z0-9]+) (?<w2>[a-z0-9]+) ([a-z0-9]+) (?<w4>[a-z0-9]+)/);
console.log(result[0]); // 出力:word1 word2 word3 word4
console.log(result[1]); // 出力:word1
console.log(result[2]); // 出力:word2
console.log(result[3]); // 出力:word3
console.log(result[4]); // 出力:word4
console.log(result.groups.w2); // 出力:word2
console.log(result.groups.w4); // 出力:word4
console.log(result.index); // 出力:5
console.log(result.input); // 出力:WORD word1 word2 word3 word4 word5
console.log(result.length); // 出力:5