Last updated: Mar 3, 2024
Reading timeยท4 min
Use the RegExp.test()
method to check if a string contains at least one
number, e.g. /\d/.test(str)
.
The test
method will return true
if the string contains at least one
number, otherwise false
is returned.
function containsNumber(str) { return /\d/.test(str); } console.log(containsNumber('hello 42 world')); // ๐๏ธ true console.log(containsNumber('hello world')); // ๐๏ธ false console.log(containsNumber('abc 123')); // ๐๏ธ true
We used the RegExp.test() method to check if the string contains numbers.
The forward slashes / /
mark the beginning and end of the regular expression.
The \d
character matches any digit from 0 to 9.
test()
method returns true
if the regular expression is matched in the string, otherwise, false
is returned.You can also use the [0-9]
range to check if the string contains numbers.
function containsNumber(str) { return /[0-9]/.test(str); } console.log(containsNumber('hello 42 world')); // ๐๏ธ true console.log(containsNumber('hello world')); // ๐๏ธ false console.log(containsNumber('abc 123')); // ๐๏ธ true
Instead of using the \d
special character, we used the [0-9]
character
class.
The character class is used to specify a range of numbers that we want to match.
[0-9]
range should be a bit more readable than the \d
character if you aren't familiar with regular expressions.If you ever need help reading a regular expression, check out this regular expression cheat sheet by MDN.
It contains a table with the name and the meaning of each special character with examples.
match()
Alternatively, you can use the match()
method.
The match()
method will return an array of the matches if the string contains
numbers, otherwise, null
is returned.
function containsNumbers(str) { return str.match(/\d/) !== null; } console.log(containsNumbers('hello 123')); // ๐๏ธ true console.log(containsNumbers('hello world')); // ๐๏ธ false console.log(containsNumbers('abc 123')); // ๐๏ธ true
The String.match() method matches a string against a regular expression.
The method returns an array containing the matches (if any) or null
if no
matches are found.
Our function checks if the match()
method didn't return null
to get a
boolean result.
If you need to get an array of the matches or null
, remove the comparison.
function containsNumbers(str) { return str.match(/\d/g); } // ๐๏ธ [ '1', '2', '3' ] console.log(containsNumbers('hello 123')); // ๐๏ธ null console.log(containsNumbers('hello world')); // ๐๏ธ [ '1', '2', '3' ] console.log(containsNumbers('abc 123'));
The first and the third strings contain numbers, so the match()
method returns
an array containing the matches.
The second string doesn't contain any numbers, so the method returns null
.
Use the test()
method to check if a string contains only digits, e.g.
/^[0-9]+$/.test(str)
.
The test
method will return true
if the string contains only digits and
false
otherwise.
function containsOnlyNumbers(str) { return /^[0-9]+$/.test(str); } console.log(containsOnlyNumbers('1234')); // ๐๏ธ true console.log(containsOnlyNumbers('123hello123')); // ๐๏ธ false console.log(containsOnlyNumbers('123.5')); // ๐๏ธ false
We used the RegExp.test() method to check if a string contains only digits.
The method returns true
if the regular expression is matched in the string and
false
otherwise.
The forward slashes / /
mark the beginning and end of the regular expression.
The caret ^
matches the beginning of the input, and the dollar sign $
matches the end of the input.
[]
is called a character class and matches a range of digits from 0
to 9
.The plus +
matches the preceding item (the 0-9
range) 1 or more times.
Note that the 0-9
range can also be written using the \d
(digit) special
character.
function containsOnlyNumbers(str) { return /^\d+$/.test(str); } console.log(containsOnlyNumbers('1234')); // ๐๏ธ true console.log(containsOnlyNumbers('123hello123')); // ๐๏ธ false
The \d
special character is equivalent to matching any digit using [0-9]
,
but I find the range to be more readable and intuitive.
If you need to also match decimal or comma-separated numbers, add the
character you'd like to match between the square brackets []
.
function containsOnlyNumbers(str) { return /^[0-9.,]+$/.test(str); } console.log(containsOnlyNumbers('1234')); // ๐๏ธ true console.log(containsOnlyNumbers('123hello123')); // ๐๏ธ false console.log(containsOnlyNumbers('123.5')); // ๐๏ธ true console.log(containsOnlyNumbers('123,5')); // ๐๏ธ true
We added a dot and a comma between the square brackets []
to also match these
characters.
You can also use capturing groups in your regular expression.
The following regex checks if the string contains only digits or digits separated by periods.
function containsOnlyNumbers(str) { return /^([0-9]+\.)*([0-9]+)$/.test(str); } console.log(containsOnlyNumbers('1234')); // ๐๏ธ true console.log(containsOnlyNumbers('12.34')); // ๐๏ธ true console.log(containsOnlyNumbers('12.34.5')); // ๐๏ธ true console.log(containsOnlyNumbers('12,34')); // ๐๏ธ false console.log(containsOnlyNumbers('12-34-56')); // ๐๏ธ false
The parentheses ()
are called a capturing group.
We used a period as the separator character, but you can use any other value, e.g. a comma.
function containsOnlyNumbers(str) { return /^([0-9]+,)*([0-9]+)$/.test(str); } console.log(containsOnlyNumbers('1234')); // ๐๏ธ true console.log(containsOnlyNumbers('12.34')); // ๐๏ธ false console.log(containsOnlyNumbers('12.34.5')); // ๐๏ธ false console.log(containsOnlyNumbers('12,34')); // ๐๏ธ true console.log(containsOnlyNumbers('12,34,56')); // ๐๏ธ true
The variable character is specified right before the first closing parenthesis -
/^([0-9]+<character>)*([0-9]+)$/
.
You can switch the character to any other value, e.g. a space.
function containsOnlyNumbers(str) { return /^([0-9]+ )*([0-9]+)$/.test(str); } console.log(containsOnlyNumbers('1234')); // ๐๏ธ true console.log(containsOnlyNumbers('12 34')); // ๐๏ธ true console.log(containsOnlyNumbers('12 34 5')); // ๐๏ธ true console.log(containsOnlyNumbers('12,34')); // ๐๏ธ false console.log(containsOnlyNumbers('12-34-56')); // ๐๏ธ false
If you ever need help reading a regular expression, check out this regular expression cheat sheet by MDN.
It contains a table with the name and the meaning of each special character with examples.
You can learn more about the related topics by checking out the following tutorials: