JavaScript文字列検索
JavaScriptの検索メソッド
- 文字列indexOf()
- 文字列lastIndexOf()
- 文字列startsWith()
- 文字列endsWith()
JavaScript文字列indexOf()
このindexOf()
メソッドはfirst
、文字列内の指定されたテキストの出現のインデックス(の位置)を返します。
例
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate");
ノート
JavaScriptはゼロから位置をカウントします。
0は文字列の最初の位置、1は2番目、2は3番目、..。
JavaScript文字列lastIndexOf()
このlastIndexOf()
メソッドは、文字列内の指定されたテキストの最後の
出現のインデックスを返します。
例
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate");
両方indexOf()
、およびlastIndexOf()
テキストが見つからない場合は-1を返します。
例
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("John");
どちらの方法も、検索の開始位置として2番目のパラメーターを受け入れます。
例
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate", 15);
lastIndexOf()
メソッドは逆方向(最後から最初まで)に検索します。つまり、2番目のパラメーターが。の場合15
、検索は位置15から開始され、文字列の先頭まで検索されます。
例
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate", 15);
JavaScript文字列search()
このsearch()
メソッドは、指定された値の文字列を検索し、一致する位置を返します。
例
let str = "Please locate where 'locate' occurs!";
str.search("locate");
気づきましたか?
2つのメソッドindexOf()
とsearch()
は等しいですか?
それらは同じ引数(パラメーター)を受け入れ、同じ値を返しますか?
2つの方法は等しくありません。違いは次のとおりです。
- この
search()
メソッドは、2番目の開始位置引数を取ることができません。 - この
indexOf()
メソッドは、強力な検索値(正規表現)を取得できません。
正規表現については、後の章で詳しく説明します。
JavaScript文字列match()
match()メソッドは、文字列で正規表現との一致を検索し、一致をArrayオブジェクトとして返します。
例1
文字列で「ain」を検索します。
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
正規表現の詳細については、JSRegExpの章を参照してください。
ノート
正規表現にg修飾子が含まれていない場合(グローバル検索を実行するため)、match()メソッドは文字列の最初の一致のみを返します。
構文
string.match(regexp)
regexp | Required. The value to search for, as a regular expression. |
Returns: | An Array, containing the matches, one item for each match, or null if no match is found |
例2
「ain」のグローバルで大文字と小文字を区別しない検索を実行します。
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
JavaScript文字列includes()
includes()
文字列に指定された値が含まれている場合、メソッドはtrueを返します。
例
let text = "Hello world, welcome to the universe.";
text.includes("world");
構文
string.includes(searchvalue, start)
searchvalue | Required. The string to search for |
start | Optional. Default 0. Position to start the search |
Returns: | Returns true if the string contains the value, otherwise false |
JS Version: | ES6 (2015) |
文字列に「world」が含まれているかどうかを確認し、12番目の位置から検索を開始します。
let text = "Hello world, welcome to the universe.";
text.includes("world", 12);
ブラウザのサポート
includes()
ES6の機能(JavaScript 2015)です。
最新のすべてのブラウザでサポートされています。
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
includes()
InternetExplorerではサポートされていません。
JavaScript文字列startsWith()
文字列が指定された値で始まる場合、メソッドは戻ります。それ以外の場合は、次startsWith()
のようになります。true
false
例
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello");
構文
string.startsWith(searchvalue, start)
パラメータ値
Parameter | Description |
---|---|
searchvalue | Required. The value to search for. |
start | Optional. Default 0. The position to start the search. |
例
let text = "Hello world, welcome to the universe.";
text.startsWith("world") // Returns false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5) // Returns false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 6) // Returns true
ノート
このstartsWith()
方法では大文字と小文字が区別されます。
ブラウザのサポート
startsWith()
ES6の機能(JavaScript 2015)です。
最新のすべてのブラウザでサポートされています。
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
startsWith()
InternetExplorerではサポートされていません。
JavaScript文字列endsWith()
文字列が指定された値で終了する場合、メソッドは戻ります。それ以外の場合は、次endsWith()
のようになります。true
false
例
文字列が「Doe」で終わっているかどうかを確認します。
var text = "John Doe";
text.endsWith("Doe");
構文
string.endswith(searchvalue, length)
パラメータ値
Parameter | Description |
---|---|
searchvalue | Required. The value to search for. |
length | Optional. The length to search. |
文字列の最初の11文字が「world」で終わっていることを確認してください。
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);
ノート
このendsWith()
方法では大文字と小文字が区別されます。
ブラウザのサポート
endsWith()
ES6の機能(JavaScript 2015)です。
最新のすべてのブラウザでサポートされています。
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
endsWith()
InternetExplorerではサポートされていません。
完全な文字列リファレンス
完全な文字列リファレンスについては、次のURLにアクセスしてください。
このリファレンスには、すべての文字列プロパティとメソッドの説明と例が含まれています。