戻る

正規表現を使った数字チェック
正規表現を使用した数字チェック

数字のみ
100
結果

小数点あり
10.5
結果

test
"3"
文字列の数字

小数点あり
3.
結果

タグ内に半角空白あり
3
結果

前ゼロあり
03
結果





copy
const b1 = document.getElementById("btn1");
b1.addEventListener("click", () => 
{
	for(i=0; i<6; i++)
	{
		const t = document.getElementById("t"+(i+1));
		const r = document.getElementById("r"+(i+1));
		r.innerText =test(t.innerText)+" length:"+t.innerText.length;
	}
});
function test(data)
{
  return /^[0-9]+$/.test(data);
}
数字チェックのサンプルに^[0-9]+$ という正規表現パターンを使用しています。
return /^[0-9]+$/.test(data);
[解説]
^[0-9]+$
^文字列の先頭に一致
[0-9]0から9までのいずれかの文字に一致
+直前の文字が1回以上繰り返される文字列に一致
$文字列の末尾に一致
test()
正規表現オブジェクトのメソッドで、指定された文字列が正規表現パターンにマッチするかどうかを調べます。
マッチすれば true を返し、マッチしなければ false を返します。
正規表現のtestメソッドの結果を判定する際には、=== (厳密等価演算子) を使用します。
== (等価演算子) でも、trueとtrue、falseとfalseは正しく比較できますが、
型変換を伴うため、意図しない結果になる可能性があります。

[その他]
javascriptで半角数字のみと,(カンマ)のみを判定する例
copy
function test(data) {
  // 半角数字とカンマのみかどうかを判定する正規表現
  const regex = /^[\d,]+$/;
  return regex.test(data);
}
^文字列の先頭に一致
[\d,]半角数字 (\d) またはカンマ (,) に一致


戻る
back

Check numbers using regular expressions Check numbers
using regular expressions

Numbers only
100
Result

Decimal point included
10.5
Result

test
"3"
Number as string

Decimal point included
3.
Result

Half-width space included in tag
3
Result

Leading zero included
03
Result





copy
const b1 = document.getElementById("btn1");
b1.addEventListener("click", () => 
{
	for(i=0; i<6; i++)
	{
		const t = document.getElementById("t"+(i+1));
		const r = document.getElementById("r"+(i+1));
		r.innerText =test(t.innerText)+" length:"+t.innerText.length;
	}
});
function test(data)
{
  return /^[0-9]+$/.test(data);
}
The regular expression pattern used for checking numbers is ^[0-9]+$.
return /^[0-9]+$/.test(data);
[Explanation]
^[0-9]+$
^Matches the beginning of the string
[0-9]Matches any character between 0 and 9
+Matches a string where the previous character is repeated one or more times
$Matches the end of the string
test()
A method of the regular expression object that checks whether a specified string matches the regular expression pattern.
If it matches, it returns true, otherwise it returns false.
When checking the result of the test method of a regular expression, use === (strict equality operator).
Even with == (equality operator), true and true, and false and false can be compared correctly,
but because it involves type conversion, it may result in unintended results.

[Other]
Example of checking only half-width numbers and only , (commas) in javascript
copy
function test(data) {
  //Regular expression to check if the string contains only half-width numbers and commas
  const regex = /^[\d,]+$/;
  return regex.test(data);
}
^Matches the beginning of the string
[\d,]Matches half-width numbers (\d) or commas (,)


back



著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。