JavaScript match 正規表現で指定桁数の数値の値を返す

構文
文字列.match (正規表現のパターン);
一致した場合、一致した値を配列で返します。
数値の4桁の値を返す
/^[0-9]{4}$/
先頭と末尾も数値を返します

使用例

const rex = /^[0-9]{4}$/;
const b = "5648".match(rex);
const c = "aa12".match(rex);
const d = "11122233".match(rex);

console.log(b[0]);//51648
console.log(c); // null 文字あり
console.log(d); // null 5桁でない

結果
> “5648"
> null
> null

JavaScript

Posted by arkgame