indexOf【順方向 文字列検索】
lastIndexOf【逆方向 文字列検索】
includes【部分文字列判定】
search【正規表現検索 (簡易)】
match【正規表現検索 (詳細)】
matchAll【正規表現検索 (詳細イテレータ)】

String.prototype.indexOf【順方向 文字列検索】

メモ

概要

外部リンク

構文

string.indexOf( searchString, position ) 
string.indexOf( searchString[, position] ) 

一致文字列位置 (詳細は下記参照)
searchString検索文字列
position検索開始位置 (0 ~ 文字列長:詳細は下記参照)
position補正値
省略0
position < 00
文字列長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【逆方向 文字列検索】

メモ

概要

外部リンク

  • ECMA-262 (英語)
    String.prototype.lastIndexOf (searchString, position)
    String.prototype.lastIndexOf ( searchString [ , position ] )
    ES2023 (14) ES2022 (13) ES2021 (12)

構文

string.lastIndexOf( searchString, position ) 
string.lastIndexOf( searchString[, position] ) 

一致文字列位置 (詳細は下記参照)
searchString検索文字列
position検索開始位置 (0 ~:indexOf() と同様 左からの位置 / 詳細は下記参照)
position補正値
省略文字列長
position < 00
文字列長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【部分文字列判定】

メモ

概要

関連

外部リンク

構文

string.includes( searchString[, position] )

判定結果
true:検索文字列を含む
false:検索文字列を含まない
searchString検索文字列 (正規表現不可)
position検索開始位置 (詳細は下記参照)

TypeError  (searchStringが正規表現)
(RegExp[ @@match ]【正規表現判定】プロパティ  をfalseに設定すれば回避可能)
position備考
0 ≦ position文字列長 先頭からの文字位置 (0:先頭)
文字列長position 文字列長
position < 00 (先頭)

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.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【正規表現検索 (詳細)】

メモ

概要

外部リンク

構文

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【正規表現検索 (詳細イテレータ)】

メモ

概要

外部リンク

構文

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"