EN
TypeScript - check if character is letter
0 points
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:
xxxxxxxxxx
1
const isLetter = (character: string): boolean => {
2
// it will work only for: most Latin, Greek, Armenian and Cyrillic scripts.
3
// it will not work for: Chinese, Japanese, Arabic, Hebrew and most other scripts.
4
return character && character.length === 1 && character.toLowerCase() !== character.toUpperCase();
5
};
or:
xxxxxxxxxx
1
const LETTER_EXPRESSION: RegExp = /^\p{L}$/u; // Supported by ES6+, Some bugs in FF < 78
2
3
const isLetter = (character: string): boolean => {
4
return character && LETTER_EXPRESSION.test(character);
5
};
Important things:
- by default, only modern TypeScript (ES6+) introduces some solutions to check if some character is a letter,
- 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,
- 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.
In this section, you will find a function that gives good enough support for many letters detection.
xxxxxxxxxx
1
// ONLINE-RUNNER:browser;
2
3
let isLetter;
4
5
try {
6
const LETTER_EXPRESSION: RegExp = /^\p{L}$/u; // Supported by ES6+, Some bugs in FF < 78
7
8
isLetter = (character: string): boolean => {
9
return character && LETTER_EXPRESSION.test(character);
10
};
11
} catch (e) {
12
console.log('Unicode flag in regular expressions are not supported. Used legacy isLetter() function.');
13
14
isLetter = (character: string): boolean => {
15
// it doesn't work for some alphabets but still enough in most cases
16
return character && character.length === 1 && character.toLowerCase() !== character.toUpperCase();
17
};
18
}
19
20
21
// Usage example:
22
23
//Note: Unicode expression is supported by modern Chrome, Safar, Edge, FF
24
25
// // Unicode expression / Case conversion (legacy)
26
27
// letters
28
29
console.log(isLetter('é')); // true / true
30
console.log(isLetter('è')); // true / true
31
console.log(isLetter('ê')); // true / true
32
console.log(isLetter('ü')); // true / true
33
console.log(isLetter('ö')); // true / true
34
console.log(isLetter('à')); // true / true
35
console.log(isLetter('a')); // true / true
36
console.log(isLetter('z')); // true / true
37
console.log(isLetter('A')); // true / true
38
console.log(isLetter('Z')); // true / true
39
console.log(isLetter('日')); // true / false
40
console.log(isLetter('я')); // true / true
41
console.log(isLetter('דֹ')); // false / false
42
43
// digits
44
45
console.log(isLetter('0')); // false / false
46
console.log(isLetter('5')); // false / false
47
console.log(isLetter('9')); // false / false
48
49
// other
50
51
console.log(isLetter('10')); // false / false
52
console.log(isLetter('ab')); // false / false
53
console.log(isLetter('😎')); // false / false
54
55
console.log(isLetter('-')); // false / false
56
console.log(isLetter('.')); // false / false
57
console.log(isLetter('+')); // false / false
58
console.log(isLetter('*')); // false / false
59