How to check if character is a letter in Javascript?

前端 未结 13 1765
渐次进展
渐次进展 2020-11-27 14:38

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

13条回答
  •  渐次进展
    2020-11-27 15:36

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

提交回复
热议问题