Movatterモバイル変換


[0]ホーム

URL:


  1. 開発者向けのウェブ技術
  2. JavaScript
  3. JavaScript リファレンス
  4. 標準組み込みオブジェクト
  5. String
  6. endsWith()

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。

View in EnglishAlways switch to English

String.prototype.endsWith()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年9月⁩.

endsWith()String 値のメソッドで、文字列が引数で指定された文字列で終わるかを判定してtruefalse を返します。

試してみましょう

const str1 = "Cats are the best!";console.log(str1.endsWith("best!"));// 予想される結果: trueconsole.log(str1.endsWith("best", 17));// 予想される結果: trueconst str2 = "Is this a question?";console.log(str2.endsWith("question"));// 予想される結果: false

構文

js
endsWith(searchString)endsWith(searchString, endPosition)

引数

searchString

str の末尾で検索される文字の集合です。正規表現にすることはできません。正規表現ではない値はすべて文字列に変換されますので、省略したりundefined を渡したりすると、endsWith()"undefined" という文字列を検索します。これはおそらく望むところではないでしょう。

endPosition省略可

searchString が見つかると期待される末尾の位置(searchString の最後の文字 +1 のインデックス)です。既定値はstr.length です。

返値

文字列が指定された文字列で終わる場合、searchString が空文字列の場合はtrue、それ以外の場合はfalse です。

例外

TypeError

searchString正規表現であった場合。

解説

文字列が特定の文字列で終わるかどうかを判断できます。このメソッドでは(英文字の)大文字小文字は区別されます。

endsWith() の使用

js
const str = "To be, or not to be, that is the question.";console.log(str.endsWith("question.")); // trueconsole.log(str.endsWith("to be")); // falseconsole.log(str.endsWith("to be", 19)); // true

仕様書

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.endswith

ブラウザーの互換性

関連情報

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp