How to check if character is a letter in Javascript?

前端 未结 13 1761
渐次进展
渐次进展 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:37

    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.

提交回复
热议问题