There are two ways to judge integers: regular judgment and verbatim judgment.
Since the verbatim judgment is too inefficient, I will not describe it here. Interested spectators can Google them themselves.
1. Regular judgment
The code copy is as follows:
var r = /^/+?[1-9][0-9]*$/; //Positive integer
console.log(r.test(1.23));
Effectiveness test:
http://jsfiddle.net/wzsdp9Lc/
Extended feature list
The code copy is as follows:
"^//d+$" //Non-negative integer (positive integer + 0)
"^[0-9]*[1-9][0-9]*$" //Positive integer
"^((-//d+)|(0+))$" //Not positive integer (negative integer + 0)
"^-[0-9]*[1-9][0-9]*$" //Negative integer
"^-?//d+$" //Integer
"^//d+(//.//d+)?$" //Non-negative floating point number (positive floating point number + 0)
"^(([0-9]+//.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*//.[0-9]+)|([0-9]*[1-9][0-9]*))$" //Positive floating point number
"^((-//d+(//.//d+)?)|(0+(//.0+)?))$" //Non-positive floating point number (negative floating point number + 0)
"^(-(([0-9]+//.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*//.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //Negative floating point number
"^(-?//d+)(//.//d+)?$" //Floating point number
2. Rounding judgment
The idea of this method is to determine whether it is equal to the original value after rounding
The code copy is as follows:
var num=1.23;
if (parseInt(num) != num) {
console.log(num+"is non-integer");
}
else{
console.log(num+"is an integer");
}
Effectiveness test
http://jsfiddle.net/euvn0L1g/1/