How do you check if a string only contains alphabets in javascript?

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.

Copied!

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 [].

Copied!

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.

Copied!

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 #

  • Remove all non-alphanumeric Characters from String in JS
  • Check if String contains only Letters and Spaces in JS

How do I check if a string contains only alphabets?

Regex can be used to check a string for alphabets. String. matches[] method is used to check whether or not the string matches the given regex.

How do you check if a string contains only alphabets and numbers in JavaScript?

To check if a string contains only letters and numbers in JavaScript, call the test[] method on this regex: /^[A-Za-z0-9]*$/ . If the string contains only letters and numbers, this method returns true . Otherwise, it returns false .

How do you know if input is only letters?

Use the str. isalpha[] method to check if a user input contains only letters, e.g. if user_input. isalpha[]: . The isalpha method will return True if all characters in the input are alphabetic and it contains at least one character.

Chủ Đề