Check if a String Contains Numbers in JavaScript
In this article, we will discuss the following topics:
- Check if a string contains numbers using the
test()
method - Check if a string contains numbers using the
search()
method - Check if a string contains numbers using the
match()
method
Check if a string contains numbers using the test() method
To check if a string contains numbers in JavaScript, we have to call the test()
method on the regular expression /\d/
and pass the string as the argument. The test()
method returns true
if the string contains at least one number, or false
otherwise.
For example:
function hasNumber(str) {
return /\d/.test(str);
}
console.log(hasNumber("iPhone 14 Pro Max")); // Output: true
console.log(hasNumber("JavaScript is awesome")); // Output: false
Let’s break down the regular expression /\d/
Regular expression starts and ends with forward slashes /
\d
Matches any digit (0-9).
We have not used a global flag g
because we want to stop the match as soon as we match the first digit. The logic is if we match the first digit, then it means the string contains a number. There is no need to continue matching after the first match.
We can also use pattern collection /[0-9]/
or /[0123456789]/
instead of /\d/
as a regular expression.
For example:
function hasNumber(str) {
return /[0-9]/.test(str);
}
console.log(hasNumber("iPhone 14 Pro Max")); // Output: true
console.log(hasNumber("JavaScript is awesome")); // Output: false
Check if a string contains numbers using the search() method
To check if a string contains numbers using the search()
method, we have to call the search()
method on the string and pass the regular expression /\d/
as the argument.
The search()
method returns the index of the first match (i.e. index of the first number in our case) if the string contains at least one number, or -1
if the string doesn’t contain any number.
For example, the statement "iPhone 14 Pro".search(/\d/);
gives us 7
because the string “iPhone 14 Pro”
contains a number at index 7
(gives the index of the first match) whereas the statement "JavaScript".search(/\d/);
gives us -1
because the string “JavaScript”
doesn’t contain any number.
Using the above logic, i.e. the search()
method returns -1
if the string doesn’t contain numbers, or the index of the first match if the string contains at least one number, we can determine if the string contains numbers.
For example:
function hasNumber(str) {
if(str.search(/\d/) === -1) {
return false;
} else {
return true;
}
}
console.log(hasNumber("iPhone 14 Pro Max")); // Output: true
console.log(hasNumber("JavaScript is awesome")); // Output: false
Check if a string contains numbers using the match() method
To check if a string contains numbers using the match()
method, we have to call the match()
method on the string and pass the regular expression /\d/
as the argument.
The match()
method returns an array of information if the string contains at least one number, or null
otherwise.
For example, the statement "iPhone 14 Pro".match(/\d/);
gives us an array ['1', index: 7, input: 'iPhone 14 Pro', groups: undefined]
containing all the information about the index, input, and capturing groups of the first match.
Whereas the statement "JavaScript".match(/\d/);
gives us null
because the string “JavaScript”
doesn’t contain any number.
Using the above logic, i.e. the match()
method returns null
, if the string doesn’t contain any number, or an array of information about the match if the string contains at least one number, we can determine whether the string contains numbers or not.
For example:
function hasNumber(str) {
if(str.match(/\d/) === null) {
return false;
} else {
return true;
}
}
console.log(hasNumber("iPhone 14 Pro Max")); // Output: true
console.log(hasNumber("JavaScript is awesome")); // Output: false
The match()
and search()
method takes the same argument, only the return value is different.
Note: Please do note that the value returned by the match()
method depends on the presence or absence of a global flag g
.
Without global flag g
:
If the global flag g
is absent, the match()
method searches for the first occurrence of the pattern in the string and returns an array containing information such as index, input, and capturing groups of the first match.
For example:
console.log("iPhone 14 Pro".match(/\d/));
Output:
['1', index: 7, input: 'iPhone 14 Pro', groups: undefined]
With global flag g
:
If the global flag g
is present, the match()
method searches for all occurrences of the pattern in the string and returns an array containing only the indexes of all the matches. It doesn’t return other information like capturing groups.
For example:
console.log("iPhone 14 Pro".match(/\d/g));
Output
:
['1', '4']