How to Check if a String Contains Only Numbers in JavaScript
To check if a string contains only numbers in JavaScript, call the test()
method on this regular expression: ^\d+$
. The test()
method will return true
if the string contains only numbers. Otherwise, it will return false
.
For example:
function containsOnlyNumbers(str) {
return /^\d+$/.test(str);
}
console.log(containsOnlyNumbers('HTML5')); // false
console.log(containsOnlyNumbers('1234')); // true
console.log(containsOnlyNumbers('3 bananas')); // false
The RegExp test() method searches for a match between a regular expression and a string.
The /
and /
characters are used to start and end a regular expression.
The ^
character marks the beginning of the string input, and the $
character marks the end of it.
The \d
pattern matches any digit (0
- 9
) in the string.
Adding the +
character after the \d
makes the regex match one or more occurrences of the \d
pattern.
So the regex matches a string that starts and ends with a consecutive sequence of digits.
We can use the [0-9]
pattern to match digits. This pattern matches any number character between 0
and 9
.
function containsOnlyNumbers(str) {
return /^[0-9]+$/.test(str);
}
console.log(containsOnlyNumbers('HTML5')); // false
console.log(containsOnlyNumbers('1234')); // true
console.log(containsOnlyNumbers('3 bananas')); // false
You might find [0-9]
to be more readable than \d
, especially if you’re not very familiar with special characters in regular expressions.
Match a string that contains numbers separated by a character
Sometimes we would like to match strings where the numbers might be separated by a particular character, such as a space or comma.
function containsOnlyNumbers(str) {
return /^(\d+,)*(\d+)$/.test(str);
}
console.log(containsOnlyNumbers('123456789')); // true (separator not required)
console.log(containsOnlyNumbers('123,456,789')); // true
console.log(containsOnlyNumbers('123-456-789')); // false
We do this using a regular expression of this format: ^(\d+{ch})*(\d+)$
, where {ch}
is the character separating the numbers.
So we can use a very similar regex to match a string that contains only numbers separated with hyphens:
function containsOnlyNumbers(str) {
return /^(\d+-)*(\d+)$/.test(str);
}
console.log(containsOnlyNumbers('123456789')); // true
console.log(containsOnlyNumbers('123,456,789')); // false
console.log(containsOnlyNumbers('123-456-789')); // true
Or spaces:
function containsOnlyNumbers(str) {
return /^(\d+ )*(\d+)$/.test(str);
}
console.log(containsOnlyNumbers('123456789')); // true
console.log(containsOnlyNumbers('123 456 789')); // true
console.log(containsOnlyNumbers('123-456-789')); // false
Tip: If you ever come across a regular expression with a pattern you find hard to understand, this regular expression cheat sheet from the MDN docs may help.
Like before, we could use [0-9]
in place of \d
for the regular expression:
function containsOnlyNumbers(str) {
return /^([0-9]+-)*([0-9]+)$/.test(str);
}
console.log(containsOnlyNumbers('123456789')); // true
console.log(containsOnlyNumbers('123,456,789')); // false
console.log(containsOnlyNumbers('123-456-789')); // true
See also
- How to Check if a String Contains Numbers in JavaScript
- How to Convert Hours and Minutes to Seconds in JavaScript
- How to Sort an Object Array by a Boolean Property in JavaScript
- How to Fix the "Unexpected reserved word (await)" Error in JavaScript
- Wow, Google's new robot can play table tennis now
- How to easily launch and deploy Laravel on Vultr