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