Check if String contains only Letters and Numbers in JS

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Check if String contains only Letters and Numbers #

Use the test() method on the following regular expression to check if a string contains only letters and numbers - /^[A-Za-z0-9]*$/. The test method will return true if the regular expression is matched in the string and false otherwise.

index.js
const str1 = 'hello42'; const str2 = 'contains spaces'; const str3 = 'with symbols !@#$%^&'; function onlyLettersAndNumbers(str) { return /^[A-Za-z0-9]*$/.test(str); } console.log(onlyLettersAndNumbers(str1)); // 👉️ true console.log(onlyLettersAndNumbers(str2)); // 👉️ false console.log(onlyLettersAndNumbers(str3)); // 👉️ false
If you need to also allow spaces, hyphens, underscores, or other characters, scroll down to the next code snippet.

We called the RegExp.test method to check if the string contains only letters and numbers.

The forward slashes / / mark the beginning and end of the regular expression.

The caret ^ matches the beginning of the input, whereas the dollar $ matches the end of the input.

The square brackets [] are called a character class. We match 3 ranges in our character class:

  • all uppercase letters A-Z
  • all lowercase letters a-z
  • all numbers 0-9

The asterisk * matches the preceding item (our character class) zero or more times.

If you need a regex cheatsheet, check out this one from MDN.

If you also need to match spaces, dashes, underscores, or any other character, add the character between the square brackets [].
index.js
const str1 = 'hello42'; const str2 = 'contains spaces'; const str3 = 'with symbols !@#$%^&'; function lettersNumbersSpacesDashes(str) { return /^[A-Za-z0-9 -]*$/.test(str); } console.log(lettersNumbersSpacesDashes(str1)); // 👉️ true console.log(lettersNumbersSpacesDashes(str2)); // 👉️ true console.log(lettersNumbersSpacesDashes(str3)); // 👉️ false

In this example we match all letters, numbers, spaces and dashes. You could extend this regex according to your use case.

If instead of checking whether the string contains only letters and numbers, you want to remove all characters that are not letters or numbers, use the replace method.

index.js
const str = 'WITH symbols !@#$%^&'; const replaced = str.replace(/[^a-z0-9]/gi, ''); console.log(replaced); // 👉️ WITHsymbols

We used the String.replace method to get a new string with all non-letters and non-numbers replaced by an empty string.

When used inside of the square brackets [], a caret ^ symbol means "not the following". It's basically a negated match.

In the example, we replace all non-letters and non-numbers with an empty string.

We used the g (global) flag because we want to match all occurrences of non letters and non-numbers in the string and not just the first occurrence.

The i flag allows us to match all letters in a case-insensitive manner.

Further Reading #

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.