I am extracting a character in a Javascript string with:
var first = str.charAt(0);
and I would like to check whether it is a letter. Stran
It's possible to know if the character is a letter or not by using a standard builtin function isNaN or Number.isNaN() from ES6:
isNaN('s') // true
isNaN('-') // true
isNaN('32') // false, '32' is converted to the number 32 which is not NaN
It retruns true if the given value is not a number, otherwise false.
I`m posting here because I didn't want to post a new question. Assuming there aren't any single character declarations in the code, you can eval() the character to cause an error and check the type of the character. Something like:
function testForLetter(character) {
try {
//Variable declarations can't start with digits or operators
//If no error is thrown check for dollar or underscore. Those are the only nonletter characters that are allowed as identifiers
eval("let " + character + ";");
let regExSpecial = /[^\$_]/;
return regExSpecial.test(character);
} catch (error) {
return false;
}
}
console.log(testForLetter("!")); //returns false;
console.log(testForLetter("5")); //returns false;
console.log(testForLetter("ن")); //returns true;
console.log(testForLetter("_")); //returns false;
// to check if the given string contain alphabets
function isPangram(sentence){
let lowerCased = sentence.toLowerCase();
let letters = "abcdefghijklmnopqrstuvwxyz";
// traditional for loop can also be used
for (let char of letters){
if (!lowerCased.includes(char)) return false;
}
return true;
}
I made a function to do this:
var isLetter = function (character) {
if( (character.charCodeAt() >= 65 && character.charCodeAt() <= 90) || (character.charCodeAt() >= 97 && character.charCodeAt() <= 122) ) {
return true;
}
else{
return false;
}
}
This basically verifies in the ASCII table if the code of the character refers to a Letter.
You can see the ASCII table on this link and compare columns DEC (where is the code) and symbol: https://www.ascii-code.com/
Note: My function is true only to "simple" letters (things like "Á", "é", "ç", "Ü" it will return false... but if you needed you can adapt this function to de other conditions).
With respect to those special characters not being taken into account by simpler checks such as /[a-zA-Z]/.test(c)
, it can be beneficial to leverage ECMAScript case transformation (toUpperCase
). It will take into account non-ASCII Unicode character classes of some foreign alphabets.
function isLetter(c) {
return c.toLowerCase() != c.toUpperCase();
}
NOTE: this solution will work only for most Latin, Greek, Armenian and Cyrillic scripts. It will NOT work for Chinese, Japanese, Arabic, Hebrew and most other scripts.
ES6 supports unicode-aware regular expressions.
RegExp(/^\p{L}/,'u').test(str)
This works for all alphabets.
Unfortunately, there is a bug in Firefox (will be fixed in version 78) that prevents this from being used universally. But if you can control your runtime environment and it supports it (e.g. Node.js), this is a straightforward, comprehensive solution.
Atlernatively, XRegExp provides a polyfill of modern regular expression to all browsers.