var snumber1 = "123456789";
var scharacter2 = "abcdefgh";


有两个字符串。我如何确保snumber1仅包含数字?
什么正则表达式?

最佳答案

var snumber1 = "123456789";
//built-in function
alert ( !isNaN ( snumber1 ) );
//regexp
alert ( /^[0-9]+$/.test ( snumber1 ) );
//another regexp
alert ( /^\d+$/.test ( snumber1 ) );
//convert to Number object
alert ( parseFloat ( snumber1 ) === Number ( snumber1 ) );

09-27 12:15