EN
TypeScript - check if string contains letters
0 points
In this article, we would like to show you how to check if string contains letters in TypeScript.
Quick solution:
xxxxxxxxxx
1
const text: string = '123a';
2
3
if (text.match(/[a-zA-Z]/g)) {
4
console.log('The string contains letter(s).');
5
}
Note:
This solution only forks for common, English letters.
In this example, we use match()
method to check if the text
string contains any letters.
xxxxxxxxxx
1
const regex: RegExp = /[a-zA-Z]/g;
2
const text: string = '12abc34';
3
4
if (text.match(regex)) {
5
console.log('The string contains letter(s).');
6
}
Note:
The
match()
method used with/g
flag returns all results matching the complete regular expression (regex
). If nothing matches returnsnull
.