Languages
[Edit]
EN

TypeScript - check if character is letter

0 points
Created by:
Paluskitten
356

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

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: string): boolean => {
  // 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: RegExp = /^\p{L}$/u; // Supported by ES6+, Some bugs in FF < 78

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

 

Important things:

  1. by default, only modern TypeScript (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 {
  const LETTER_EXPRESSION: RegExp = /^\p{L}$/u; // Supported by ES6+, Some bugs in FF < 78

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

  isLetter = (character: string): boolean => {
    // 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. TypeScript - isLetter function
  2. TypeScript - 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