To check if a character is a letter in JavaScript, lowercase the character and compare it to the uppercased version, i.e., char.toLowerCase() !== char.toUpperCase()
. If the result is false
, the character is a letter. Otherwise, it isn’t.
For example:
function isCharLetter(char) {
return char.toLowerCase() !== char.toUpperCase();
}
console.log(isCharLetter('a')); // true
console.log(isCharLetter('E')); // true
console.log(isCharLetter('d')); // true
console.log(isCharLetter('-')); // false
console.log(isCharLetter('?')); // false
Note: This only works for most Latin, Greek, Armenian, and Cyrillic scripts. It won’t work for Chinese, Japanese, Arabic, Hebrew, or most other scripts.
One use for checking if a character is a letter is to validate forms. For example, if you have a form where a user needs to enter their name, you might want to ensure that only letters are entered, and not numbers or special characters.
We know that Latin letters have lowercase and uppercase forms that are different, so we take advantage of this using inequality.
console.log('z'.toLowerCase() !== 'z'.toUpperCase()); // true
console.log('ж'.toLowerCase() !== 'ж'.toUpperCase()); // true
Non-letters, on the other hand, don’t have lowercase and uppercase forms; both toLowerCase()
and toUpperCase()
always give the same result.
console.log('.'.toLowerCase() === '.'.toUpperCase()); // true
console.log('$'.toLowerCase() === '$'.toUpperCase()); // true
console.log('='.toLowerCase() === '='.toUpperCase()); // true
Check if character is letter with regex
Alternatively, we can check if a character is a letter in JavaScript by calling the test()
method on this regex: /^[a-z]$/i
.
function isCharLetter(char) {
return /^[a-z]$/i.test(char);
}
console.log(isCharLetter('g')); // true
console.log(isCharLetter('Q')); // true
console.log(isCharLetter('word')); // false
console.log(isCharLetter('_')); // false
console.log(isCharLetter('$')); // false
The RegExp
test()
method checks if a string matches the regular expression you specify. It returns true
if there is a match, and false
if there isn’t.
The forward slashes / /
marks the start and end of the regular expression.
The ^
(caret) symbol indicates that the regex match must start from the beginning of the string. In this case, the letter must be the string’s first character.
The square brackets define a character set, which is a list of characters that can match any character at that position in the input string. In this case, [a-z]
matches any single lowercase letter from a
to z
.
The $
symbol indicates that the regex pattern must be at the very end of the string. In this case, the letter must be the string’s last character. Combining ^
an $
in this way, ensures that the string is a single character.
The i
flag lets us perform a case-insensitive search for the pattern. With i
, we can omit the A-Z
range that matches uppercase letters.
For a comprehensive guide to regular expression syntax, check out this cheat sheet from the MDN docs
Check if string contains letters
To instead check if a string contains any letters, we’ll use a different regex: /[a-z]/i
function containsLetter(str) {
return /[a-z]/i.test(str);
}
console.log(containsLetter('code')); // true
console.log(containsLetter('Q')); // true
console.log(containsLetter('word')); // true
console.log(containsLetter('_')); // false
console.log(containsLetter('$')); // false
This is similar to the previous regex, except that we’ve removed the $
and ^
so that the search can occur at any point in the string, and a multi-character string containing at least one letter can be matched.
Check if string contains only letters
For checking if the string contains only letters, we’ll bring the $
and ^
back, and add a +
before $
:
function containsOnlyLetters(str) {
return /^[a-z]+$/i.test(str);
}
console.log(containsOnlyLetters('code')); // true
console.log(containsOnlyLetters('word')); // true
console.log(containsOnlyLetters('coding_beauty')); // false
console.log(containsOnlyLetters('s p a c e s')); // false
console.log(containsOnlyLetters('8pm')); // false
With $
and ^
back again, the pattern is only matched if it’s the only pattern in the string. [a-z]+
only matches a consecutive series of letters. So altogether, the regex only matches strings that contain a continuous series of letters from the start to the end of the string.
Key takeaways
- To check if a character is a letter, compare its lowercased and uppercased versions using
char.toLowerCase() !== char.toUpperCase()
. We can only regex by callingtest()
on /^[a-z]$/i. - To check if a string contains any letters, use the regex
/[a-z]/i
. - To check if a string contains only letters, use the regex
/^[a-z]+$/i
.
Every Crazy Thing JavaScript Does
A captivating guide to the subtle caveats and lesser-known parts of JavaScript.