Check if a String contains Numbers in JavaScript

avatar

Borislav Hadzhiev

4 min

banner

Photo from Unsplash

Table of Contents #

  1. Check if a String contains Numbers in JavaScript
  2. Check if String contains only Numbers in JavaScript

Check if a String contains Numbers in JavaScript #

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.

index.js
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.

The 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.

index.js
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.

The [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 cheatsheet by MDN.

It contains a table with the name and the meaning of each special character with examples.

Check if a string contains numbers using match() #

Use the String.match() method to check if a string contains numbers, e.g. str.match(/\d/) !== null;.

The match() method will return an array of the matches if the string contains numbers, otherwise null is returned.

index.js
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.

index.js
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.

Check if String contains only Numbers in JavaScript #

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.

index.js
function containsOnlyNumbers(str) { return /^[0-9]+$/.test(str); } console.log(containsOnlyNumbers('1234')); // ๐Ÿ‘‰๏ธ true console.log(containsOnlyNumbers('123hello123')); // ๐Ÿ‘‰๏ธ false console.log(containsOnlyNumbers('123.5')); // ๐Ÿ‘‰๏ธ false
If you need to match decimal or comma-separated numbers, scroll down to the next code snippet.

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.

The part between the square brackets [] 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.

index.js
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.

Matching numbers separated by a character, e.g. decimal or comma-separated #

If you need to also match decimal or comma-separated numbers, add the character you'd like to match between the square brackets [].

index.js
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.

If your use case requires other characters, simply add them between the square brackets.

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.

index.js
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.

index.js
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.

index.js
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 cheatsheet by MDN.

It contains a table with the name and the meaning of each special character with examples.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev