check if string contains number javascript code example
Example 1: javascript is variable a string
if (typeof myVar === 'string'){
}
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 string includes numbers
function containsNumbers(str){
var regexp = /\d/g;
return regexp.test(str);
};
containsNumbers("Here are some Numbers 2123");
containsNumbers("Here are no Numbers");
Example 5: how to check if a string contains numbers in javascript
function hasNumbers(str){
var regexp = /d/g;
return regexp.test(str);
};
hasNumbers("I am 20years old");
hasNumbers("I am a boy");