Languages
[Edit]
EN

JavaScript - check if character is letter

18 points
Created by:
Root-ssh
175020

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

Hint: in the below example you will find a solution that has support for i18n for many alphabets.

It is like \w regular expression group with i18n characters.

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 enough,
  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.

Reusable function example

In this section, you will find a function that gives good enough support for many letters detection.

// ONLINE-RUNNER:browser;

let isLetter;

try {
    // Unicode expression:
    const LETTER_EXPRESSION = /^\p{L}$/u; // Supported by ES6+, Some bugs in FF < 78
    isLetter = (character) => {
        return character && LETTER_EXPRESSION.test(character);
    };
} catch(e) {
    // Case conversion (legacy):
  	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

 

Alternative titles

  1. JavaScript - isLetter function
  2. JavaScript - check if character is letter with i18n
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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