indexOf【順方向 文字列検索】
lastIndexOf【逆方向 文字列検索】
includes【部分文字列判定】
search【正規表現検索 (簡易)】
match【正規表現検索 (詳細)】
matchAll【正規表現検索 (詳細イテレータ)】
String.prototype.indexOf【順方向 文字列検索】
メモ
概要
- 文字列を順方向 (前 → 後)で検索し、一致位置を返却
- 逆方向の検索:lastIndexOf【逆方向 文字列検索】 (一致位置の返却)
- 検索開始位置の指定:includes【部分文字列判定】 (結果のみ)
- 正規表現検索:
- search【正規表現検索 (簡易)】 (単一検索)
- match【正規表現検索 (詳細)】 (複数検索可)
- matchAll【正規表現検索 (詳細イテレータ)】 (複数検索可)
- RegExp【正規表現】オブジェクト のメソッド
外部リンク
- ECMA-262 (英語)
String.prototype.indexOf (searchString, position)
String.prototype.indexOf ( searchString [ , position ] )ES2024 (15) ES2023 (14) ES2022 (13)
構文
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【正規表現検索 (詳細)】 (複数検索可)
- matchAll【正規表現検索 (詳細イテレータ)】 (複数検索可)
- RegExp【正規表現】オブジェクト のメソッド
外部リンク
- ECMA-262 (英語)
String.prototype.lastIndexOf (searchString, position)
String.prototype.lastIndexOf ( searchString [ , position ] )ES2024 (15) ES2023 (14) ES2022 (13)
構文
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【正規表現検索 (詳細)】 (複数検索可)
- matchAll【正規表現検索 (詳細イテレータ)】 (複数検索可)
- RegExp【正規表現】オブジェクト のメソッド
関連
外部リンク
- ECMA-262 (英語)
String.prototype.includes ( searchString [ , position ] ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
string.includes( searchString[, position] )
判定結果
true:検索文字列を含む
false:検索文字列を含まない
searchString検索文字列 (正規表現不可)
position検索開始位置 (詳細は下記参照)
TypeError (searchStringが正規表現)
(RegExp[ @@match ]【正規表現判定】プロパティ をfalseに設定すれば回避可能)
position | 備考 |
---|---|
0 ≦ position < 文字列長 | 先頭からの文字位置 (0:先頭) |
文字列長 < position | 文字列長 |
position < 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【正規表現検索 (詳細)】 (複数検索可)
- matchAll【正規表現検索 (詳細イテレータ)】 (複数検索可)
- その他検索
- indexOf【順方向 文字列検索】 (一致位置の返却)
- lastIndexOf【逆方向 文字列検索】 (一致位置の返却)
- includes【部分文字列判定】 (検索開始位置の指定・結果のみ)
外部リンク
- ECMA-262 (英語)
String.prototype.search ( regexp ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
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 ] 【検索 (詳細)】を呼び出し
- 参照:検索フラグ・正規表現パターン
- 簡易な正規表現の検索
- その他検索
- matchAll【正規表現検索 (詳細イテレータ)】 (詳細結果のイテレータの返却)
- indexOf【順方向 文字列検索】 (一致位置の返却)
- lastIndexOf【逆方向 文字列検索】 (一致位置の返却)
- includes【部分文字列判定】 (検索開始位置の指定・結果のみ)
外部リンク
- ECMA-262 (英語)
String.prototype.match ( regexp ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
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
String.prototype.matchAll【正規表現検索 (詳細イテレータ)】
メモ
概要
- 詳細な正規表現の検索 (戻り値:検索結果のイテレータ)
- 'g'(グローバル検索) フラグ指定は必須 (未指定:TypeError 例外)
- 内部から RegExp[ @@matchAll ] 【検索 (詳細イテレータ)】を呼び出し
- 参照:検索フラグ・正規表現パターン
- 簡易な正規表現の検索
- その他検索
- match【正規表現検索 (詳細)】 (詳細結果の返却)
- indexOf【順方向 文字列検索】 (一致位置の返却)
- lastIndexOf【逆方向 文字列検索】 (一致位置の返却)
- includes【部分文字列判定】 (検索開始位置の指定・結果のみ)
外部リンク
- ECMA-262 (英語)
String.prototype.matchAll ( regexp ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
string.matchAll( regexp )
検索結果のイテレータ (詳細は下記参照)
regexpRegExp【正規表現】オブジェクト (正規表現リテラル) (それ以外は作成)
TypeError 'g'(グローバル検索) フラグ未指定
検索結果の説明 |
---|
[0]:一致文字列 [1] ~:キャプチャ文字列 (名前付きキャプチャも含む) groups:名前付きキャプチャの連想配列 (キャプチャ名・キャプチャ文字列) index:マッチ位置 (0~) input:入力の被検索文字列 length:配列数 |
例
// 0123456789+123456789+123456789+123456789+123
const str = "The quick brown fox jumps over the lazy dog.";
let regexp = /([a-z]*)o([a-z]*)/g;
// RegExp.exec()
let match;
while ((match = regexp.exec(str)) !== null) {
let output = "index = " + match.index + ", length = " + match.length;
output += ", '" + match[0] + "'";
for (let i = 1; i < match.length; i++) {
output += " / '" + match[i] + "'";
}
console.log(output);
}
// 出力:index = 10, length = 3, 'brown' / 'br' / 'wn'
// 出力:index = 16, length = 3, 'fox' / 'f' / 'x'
// 出力:index = 26, length = 3, 'over' / '' / 'ver'
// 出力:index = 40, length = 3, 'dog' / 'd' / 'g'
// String.matchAll()
let matches = str.matchAll(regexp);
for (const match of matches) {
let output = "index = " + match.index + ", length = " + match.length;
output += ", '" + match[0] + "'";
for (let i = 1; i < match.length; i++) {
output += " / '" + match[i] + "'";
}
console.log(output);
}
// 出力:index = 10, length = 3, 'brown' / 'br' / 'wn'
// 出力:index = 16, length = 3, 'fox' / 'f' / 'x'
// 出力:index = 26, length = 3, 'over' / '' / 'ver'
// 出力:index = 40, length = 3, 'dog' / 'd' / 'g'
// 名前付きキャプチャ
regexp = /(?<head>[a-z]*)o(?<tail>[a-z]*)/g;
matches = str.matchAll(regexp);
for (const match of matches) {
let output = "index = " + match.index + ", length = " + match.length;
output += ", '" + match[0] + "'";
for (let i = 1; i < match.length; i++) {
output += " / '" + match[i] + "'";
}
console.log(output);
console.log(`match.groups.head = "${match.groups.head}"`);
console.log(`match.groups['tail'] = "${match.groups['tail']}"`);
}
// 出力:index = 10, length = 3, 'brown' / 'br' / 'wn'
// 出力:match.groups.head = "br"
// 出力:match.groups['tail'] = "wn"
// 出力:index = 16, length = 3, 'fox' / 'f' / 'x'
// 出力:match.groups.head = "f"
// 出力:match.groups['tail'] = "x"
// 出力:index = 26, length = 3, 'over' / '' / 'ver'
// 出力:match.groups.head = ""
// 出力:match.groups['tail'] = "ver"
// 出力:index = 40, length = 3, 'dog' / 'd' / 'g'
// 出力:match.groups.head = "d"
// 出力:match.groups['tail'] = "g"