「JavaScript」testメソッド正規表現で末尾の文字列を指定するサンプル

書式
変数名= /文字列$/;
変数名.test(対象文字列);
末尾が特定文字を指定しています。
正規表現のパターンで文字列を検索します。
一致した場合はtrueを返し、一致しなかった場合falseを返します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const rr = /me$/;
console.log(rr.test("arkgame"));
console.log(rr.test("tme"));
console.log(rr.test("cmed"));
const rr = /me$/; console.log(rr.test("arkgame")); console.log(rr.test("tme")); console.log(rr.test("cmed"));
const rr = /me$/;

console.log(rr.test("arkgame"));
console.log(rr.test("tme"));
console.log(rr.test("cmed"));

実行結果
true
true
false

JavaScript

Posted by arkgame