JavaScript: endsWith メソッド vs indexOf メソッド vs substr メソッド vs 正規表現
JavaScriptで文字列の末尾判定を行う "endsWith" メソッド
JavaScriptの String
オブジェクトには、endsWith
メソッドという便利な機能があります。これは、文字列が特定の文字列で終わっているかどうかを判定するものです。
使い方
endsWith
メソッドは、以下の構文で使用します。
str.endsWith(searchString, [length]);
str
: 判定対象の文字列searchString
: 判定したい文字列length
: 省略可能。判定対象の文字列の末尾から検索する長さを指定します。
例
const str = "Hello, world!";
// "world!"で終わっているか判定
console.log(str.endsWith("world!")); // true
// "world"で終わっているか判定
console.log(str.endsWith("world")); // false
// 末尾から5文字以内で"world!"で終わっているか判定
console.log(str.endsWith("world!", 5)); // true
ポイント
- 大文字と小文字は区別されます。
- 空白文字も判定対象となります。
- 正規表現は使用できません。
length
パラメータは、省略すると文字列全体が対象となります。
補足
endsWith
メソッドは、ES6で導入された比較的新しい機能です。古いブラウザでは使用できない可能性があるので、必要に応じてポリフィルを用意する必要があります。
例1:ファイルパスの拡張子を判定する
function isJavaScriptFile(filePath) {
return filePath.endsWith(".js");
}
const filePath = "script.js";
if (isJavaScriptFile(filePath)) {
console.log("JavaScriptファイルです");
} else {
console.log("JavaScriptファイルではありません");
}
例2:文字列の末尾に特定の文字列が含まれているか判定する
const str = "This is a test string.";
// 末尾に"."が含まれているか判定
console.log(str.endsWith(".")); // true
// 末尾に"?"が含まれているか判定
console.log(str.endsWith("?")); // false
例3:部分一致判定
const str = "Hello, world!";
// "world"を含むか判定
console.log(str.endsWith("world")); // true
// "world!"を含むか判定
console.log(str.endsWith("world!")); // true
例4:大文字と小文字の区別
const str = "Hello, world!";
// "world!"を含むか判定
console.log(str.endsWith("world!")); // true
// "WORLD!"を含むか判定
console.log(str.endsWith("WORLD!")); // false
例5:空白文字の判定
const str = "Hello, world! ";
// "world!"を含むか判定
console.log(str.endsWith("world!")); // false
// "world! "を含むか判定
console.log(str.endsWith("world! ")); // true
例6:lengthパラメータの使用
const str = "Hello, world!";
// 末尾から5文字以内で"world!"を含むか判定
console.log(str.endsWith("world!", 5)); // true
// 末尾から4文字以内で"world!"を含むか判定
console.log(str.endsWith("world!", 4)); // false
これらのサンプルコードを参考に、endsWith
メソッドをさまざまな場面で活用してみてください。
JavaScriptで文字列の末尾判定を行う他の方法
indexOf メソッド
indexOf
メソッドは、文字列内での指定された文字列の最初の出現位置を返します。このメソッドを使用して、以下の方法で末尾判定を行うことができます。
function endsWith(str, searchString) {
const index = str.indexOf(searchString);
return index === str.length - searchString.length;
}
const str = "Hello, world!";
console.log(endsWith(str, "world!")); // true
console.log(endsWith(str, "world")); // false
indexOf
メソッドは、見つからない場合は-1
を返します。- 末尾判定を行うためには、検索結果が文字列長の末尾と同じ位置であることを確認する必要があります。
function endsWith(str, searchString) {
const substring = str.substr(str.length - searchString.length);
return substring === searchString;
}
const str = "Hello, world!";
console.log(endsWith(str, "world!")); // true
console.log(endsWith(str, "world")); // false
substr
メソッドは、開始位置と切り出す長さを指定する必要があります。- 末尾判定を行うためには、開始位置を文字列長から検索文字列長を引いた値に設定する必要があります。
正規表現を使用して、文字列の末尾判定を行うこともできます。
function endsWith(str, searchString) {
const regex = new RegExp(searchString + "$");
return regex.test(str);
}
const str = "Hello, world!";
console.log(endsWith(str, "world!")); // true
console.log(endsWith(str, "world")); // false
- 正規表現を使用するには、
RegExp
オブジェクトを作成する必要があります。 - 末尾判定を行うためには、末尾にアンカー文字 (
$
) を使用する必要があります。
比較演算子
文字列の長さを比較することで、末尾判定を行うこともできます。
function endsWith(str, searchString) {
return str.length >= searchString.length && str.substr(str.length - searchString.length) === searchString;
}
const str = "Hello, world!";
console.log(endsWith(str, "world!")); // true
console.log(endsWith(str, "world")); // false
- 文字列の長さを比較する前に、検索文字列の長さが文字列長よりも長くないことを確認する必要があります。
- 末尾判定を行うためには、
substr
メソッドを使用して、末尾の部分文字列を取得し、検索文字列と比較する必要があります。
- 読みやすさや分かりやすさを重視する場合は、
endsWith
メソッドを使用することをおすすめします。 - 処理速度を重視する場合は、
indexOf
メソッドやsubstr
メソッドを使用することをおすすめします。 - 複雑なパターン判定を行う場合は、正規表現を使用することをおすすめします。
それぞれの方法のメリットとデメリットを理解した上で、目的に合った方法を選択してください。
javascript string ends-with