Check if String contains only Letters and Numbers in JS

avatar

Borislav Hadzhiev

Wed Oct 20 20212 min read

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 or dashes or underscores, etc, scroll down to the next example.

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

The forward slashes / / denote the start 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 basically 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. It's by far the best one out there.

If you also need to match spaces or dashes or underscores, etc, all you have to do is 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 similarly extend this to suit 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 or 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 numbers with an empty string.

We use the g (global) flag, because we want to match all occurrences of non letters and numbers in the string.

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

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee