js check number in string code example
Example 1: javascript check if number
function isNumber(n) { return /^-?[\d.]+(?:e-?\d+)?$/.test(n); }
------------------------
isNumber('123');
isNumber('123abc');
isNumber(5);
isNumber('q345');
isNumber(null);
isNumber(undefined);
isNumber(false);
isNumber(' ');
Example 2: check if a string contains digits js
function hasNumber(myString) {
return /\d/.test(myString);
}
Example 3: js string have number js
var hasNumber = /\d/;
hasNumber.test("ABC33SDF");
hasNumber.test("ABCSDF");
Example 4: javascript check if variable is number
function isNumber(n) {
return !isNaN(parseFloat(n)) && !isNaN(n - 0);
}