JavaScript exec正規表現で先頭の文字列(^)のサンプル

環境
Windows 10 home 64bit
Google Chrome 107.0.5304.122

書式
const 変数名 = /^文字/;
符号(^)を使って、先頭の文字列を指定しています。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const str = /^ST/;
const res = str.exec("STUDY");
console.log(res[0]);
const str = /^ST/; const res = str.exec("STUDY"); console.log(res[0]);
const str = /^ST/;
const res = str.exec("STUDY");

console.log(res[0]);

実行結果
> “ST"

JavaScript

Posted by arkgame