To check if a string contains numbers in JavaScript, call the test()
method on this regex: /\d/
. test()
will return true
if the string contains numbers. Otherwise, it will return false
.
For example:
function containsNumbers(str) {
return /\d/.test(str);
}
console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
The RegExp test() method searches for a match between a regular expression and a string.
The /
and /
characters are used to start and end the regular expression.
The \d
metacharacter matches any digit (0
– 9
) in the string.
You can also use the [0-9]
to match digits. This pattern matches any number character between 0
and 9
.
function containsNumbers(str) {
return /[0-9]/.test(str);
}
console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
You might find [0-9]
to be more readable than using \d
, especially if you’re not very familiar with special characters in regular expressions.
Check if string contains only numbers
To check if the string contains only numbers, we’ll have to use a different regular expression – ^\d+$
:
function containsOnlyNumbers(str) {
return /^\d+$/.test(str);
}
console.log(containsOnlyNumbers('hello123')); // false
console.log(containsOnlyNumbers('3453')); // true
console.log(containsOnlyNumbers('3 apples')); // false
The ^
character marks the beginning of the string input, and the $
character marks the end of it.
Adding the +
character after the \d
makes regex match one or more occurrences of the \d
pattern.
So the regex matches a string starting and ending with a consecutive sequence of digits.
As we did before, we could replace \d
with [0-9]
here:
function containsOnlyNumbers(str) {
return /^[0-9]+$/.test(str);
}
console.log(containsOnlyNumbers('hello123')); // false
console.log(containsOnlyNumbers('3453')); // true
console.log(containsOnlyNumbers('3 apples')); // false
The String match() method
We can use the String
match()
method in place of RegExp
test()
to check if a string contains numbers
function containsNumbers(str) {
return Boolean(str.match(/\d/));
}
console.log(containsNumbers('hello123')); // true
console.log(containsNumbers('javascript')); // false
console.log(containsNumbers('3 apples')); // true
The String match() method returns an array of all the matches of a regular expression in a string. If there are no matches, it returns null
.
function containsNumbers(str) {
return str.match(/\d/);
}
console.log(containsNumbers('hello123'));
// [ '1', index: 5, input: 'hello123', groups: undefined ]
console.log(containsNumbers('javascript')); // null
console.log(containsNumbers('3 apples'));
// [ '3', index: 0, input: '3 apples', groups: undefined ]
We pass the result of match()
to the Boolean()
constructor to convert it to a Boolean
value. Boolean()
converts truthy values to true
, and falsy values to false
.
In JavaScript, there are six falsy values: undefined
, null
, NaN
, 0
, ''
(empty string), and false
. Every other value is truthy.
console.log(Boolean(undefined)); // false
console.log(Boolean(['number60'])); // true
console.log(Boolean(null)); // false
console.log(Boolean(5)); // true
11 Amazing New JavaScript Features in ES13
This guide will bring you up to speed with all the latest features added in ECMAScript 13. These powerful new features will modernize your JavaScript with shorter and more expressive code.