这是验证整数的解决方案。有人可以解释一下Karim's answer的逻辑 这很有效,但我无法理解。
var intRegex = /^\d+$/;
if(intRegex.test(someNumber)) {
alert('I am an int');
...
}
答案 0 :(得分:13)
正则表达式:/^\d+$/
^ // beginning of the string
\d // numeric char [0-9]
+ // 1 or more from the last
$ // ends of the string
当它们全部合并时:
从字符串的开头到结尾有一个或多个数字char [0-9]和数字。
答案 1 :(得分:5)
查看正则表达式参考:http://www.javascriptkit.com/javatutors/redev2.shtml
/^\d+$/
^ : Start of string
\d : A number [0-9]
+ : 1 or more of the previous
$ : End of string
答案 2 :(得分:-1)
此正则表达式可能更好/^[1-9]+\d*$/
^ // beginning of the string
[1-9] // numeric char [1-9]
+ // 1 or more occurrence of the prior
\d // numeric char [0-9]
* // 0 or more occurrences of the prior
$ // end of the string
还将测试预填充零的非负整数