Languages
[Edit]
EN

JavaScript - check if string contains letters

0 points
Created by:
Zayaan-Rasmussen
543

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 /g flag returns all results matching the complete regular expression (regex). If nothing matches returns null.

See also

  1. JavaScript - check if character is letter with i18n

References

  1. String.prototype.match() - JavaScript | MDN

Alternative titles

  1. JavaScript - check if string contains any letter
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