Languages
[Edit]
EN

JavaScript - check if character is letter

6 points
Created by:
Root-ssh
103930

In this short article, we would like to show how to check if the character is a letter using JavaScript.

Quick solutions:

const isLetter = (character) => {
    // it will work only for: most Latin, Greek, Armenian and Cyrillic scripts.
    // it will not work for: Chinese, Japanese, Arabic, Hebrew and most other scripts.
    return character && character.length === 1 && character.toLowerCase() !== character.toUpperCase();
};

 or:

const LETTER_EXPRESSION = /^\p{L}$/u; // Supported by ES6+, Some bugs in FF < 78

const isLetter = (character) => {
    return character && LETTER_EXPRESSION.test(character);
};

 

Important things:

  1. by default, only modern JavaScript (ES6+) introduces some solutions to check if some character is a letter,
  2. if ES6+ is not supported, it is possible to use case conversion to make some letter detection - it doesn't work in all cases but can be enought,
  3. the third option is to use an external library: XRegExp or unicode-properties (with getCategory()) - but it increases bundle size, what not accepted on some websites.

Practical example:

// ONLINE-RUNNER:browser;

let isLetter;

try {
    const LETTER_EXPRESSION = /^\p{L}$/u; // Supported by ES6+, Some bugs in FF < 78

    isLetter = (character) => {
        return character && LETTER_EXPRESSION.test(character);
    };
} catch(e) {
  	console.log('Unicode flag in regular expressions are not supported. Used legacy isLetter() function.');

    isLetter = (character) => {
      	// it doesn't work for some alphabets but still enough in most cases
        return character && character.length === 1 && character.toLowerCase() !== character.toUpperCase();
    };
}

// Usage example:

//Note: Unicode expression is supported by modern Chrome, Safar, Edge, FF

//                            // Unicode expression / Case conversion (legacy)

// letters

console.log(isLetter('é'));   // true               / true
console.log(isLetter('è'));   // true               / true
console.log(isLetter('ê'));   // true               / true
console.log(isLetter('ü'));   // true               / true
console.log(isLetter('ö'));   // true               / true
console.log(isLetter('à'));   // true               / true
console.log(isLetter('a'));   // true               / true
console.log(isLetter('z'));   // true               / true
console.log(isLetter('A'));   // true               / true
console.log(isLetter('Z'));   // true               / true
console.log(isLetter('日'));  // true               / false
console.log(isLetter('я'));   // true               / true
console.log(isLetter('דֹ'));   // false              / false

// digits

console.log(isLetter('0'));  // false               / false
console.log(isLetter('5'));  // false               / false
console.log(isLetter('9'));  // false               / false

// other

console.log(isLetter('10')); // false               / false
console.log(isLetter('ab')); // false               / false
console.log(isLetter('😎')); // false               / false

console.log(isLetter('-'));  // false               / false
console.log(isLetter('.'));  // false               / false
console.log(isLetter('+'));  // false               / false
console.log(isLetter('*'));  // false               / false
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join