Languages
[Edit]
EN

JavaScript - check if string contains letters

0 points
Created by:
Zayaan-Rasmussen
323

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
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