Check if string consists of only alphabets in JavaScript
This post will discuss how to check if a string consists of only alphabets from A to Z, either in uppercase or lowercase, in JavaScript.
There are several ways to check if a string consists of only alphabets in JavaScript. Here are some of the most common functions, along with some examples and explanations:
1. Using String.match() function
One of the most common ways is to use a regular expression that matches only alphabets. The regular expression /^[a-zA-Z]+$/ matches any string that contains one or more alphabets from A to Z, in upper or lower case. The ^ character indicates the start of the string, while the $ character indicates the end of the string. We can use the match() function of the string object to check if a string matches the pattern. This function takes a regular expression as an argument and returns an array containing the matched substrings, or null if no match is found. Here’s an example:
|
1 2 3 4 5 6 7 8 |
let str = "Hello"; let result = str.match(/^[a-zA-Z]+$/); // ["Hello"] if (result) { console.log("The string consists of only alphabets"); } else { console.log("The string has non-alpha characters"); } |
2. Using RegExp.test() function
The test() function of the regular expression object takes a regular expression as an argument and returns true if it matches a string, or false otherwise. It can be used to check whether a string matches a pattern or not. We can use it with the same regular expression as above to check if a string consists of only alphabets. The regular expression /^[a-zA-Z]+$/ will return true if the string contains only alphabets, and false otherwise. For example, to check if the string "Hello" contains alpha characters, we can do like:
|
1 2 3 4 5 6 7 8 |
let str = "Hello"; let result = /^[a-zA-Z]+$/.test(str); // true if (result) { console.log("The string consists of only alphabets"); } else { console.log("The string has non-alpha characters"); } |
3. Using String.search() function
The String.search() function takes a regular expression as an argument and returns the index of the first match in a string, or -1 if no match is found. We can use the same regular expression as above to check if a string consists of only alphabets. Here’s an example:
|
1 2 3 4 5 6 7 8 |
let str = "Hello"; let result = str.search(/^[a-zA-Z]+$/); // 0 if (result === 0) { console.log("The string consists of only alphabets"); } else { console.log("The string has non-alpha characters"); } |
4. Using String.charCodeAt() function
The String.charCodeAt() function returns the Unicode value of a character at a specified index in a string. We can use this function to check if a string contains alpha characters by looping through each character in the string and comparing its Unicode value with the ranges of alpha characters. For example, the Unicode value for 'a' is 97 and for 'z' is 122, and for 'A' is 65 and for 'Z' is 90. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function isAlpha(str) { for (let i = 0; i < str.length; i++) { // Get the Unicode value of the current character let charCode = str.charCodeAt(i); // Check if the Unicode value is not in the ranges of alpha characters if (!(charCode > 64 && charCode < 91) && // upper alpha (A-Z) !(charCode > 96 && charCode < 123)) { // lower alpha (a-z) return false; } } return true; } let str = "Hello"; let result = isAlpha(str); // true if (result === true) { console.log("The string consists of only alphabets"); } else { console.log("The string has non-alpha characters"); } |
That’s all about checking if a string consists of only alphabets in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)