function hasNumber( str ){
return /\d/.test( str );
}
results:
hasNumber(0) // true
hasNumber("0") // true
hasNumber(2) // true
hasNumber("aba") // false
hasNumber("a2a") // true
While the above will return truthy as soon the function encounters one Number \d
if you want to be able return an Array of the matched results:
function hasNumber( str ){
return str.match( /\d+/g );
}
results:
hasNumber( "11a2 b3 c" ); // ["11", "2", "3"]
hasNumber( "abc" ); // null
if( hasNumber( "a2" ) ) // "truthy statement"
where +
in /\d+/g
is to match one or more Numbers (therefore the "11" in the above result) and the g
Global RegExp flag is to continue iterating till the end of the passed string.
Advanced_Searching_With_Flags
RegExp.prototype.test()
String.prototype.match()