match()
or test()
str.match(/[0-9]/)
the result is non-empty if str
has at least one number. In this example, it is necessary that all characters in str
have been numbersvar str = '123';
var strIsValid = !!str.match(/[0-9]/) && str.match(/[0-9]/).input === str;
/^[0-9]+$/
/^[0-9]$/.test(str)
parseInt(str, 10).toString() === str
Find more questions by tags Regular expressionsJavaScript
but the characters in the string is clearly not numeric - Willie.Berge commented on June 8th 19 at 16:54
(str >>> 0).toString() === str
by the way the version is also minus missed. - Marty93 commented on June 8th 19 at 16:57
("01" >>> 0).toString() === "01" // false
Again - Willie.Berge commented on June 8th 19 at 17:00