Check if first letter of word is a capital letter

后端 未结 8 1213
春和景丽
春和景丽 2020-12-07 22:07

Is it possible in Javascript to find out if the first letter of a word is a capital letter?

相关标签:
8条回答
  • 2020-12-07 23:04

    The other answers on this page are fine for strings that are known to only contain non-accented A-Z letters. If you can't guarantee this (e.g. user input), they may give unexpected results: false positives for uncapitalisable initials like "1940s" or "中文", or false negatives for accented or non-Roman capital initials like "Łukasz" or "Александра".

    This variant returns true if the initial is any capital letter, and only if it's a capital letter:

    function initialIsCapital( word ){
      return word[0] !== word[0].toLowerCase();
    }
    

    Use .charAt(0) instead of [0] if you need IE8 support. Which is faster varies between browsers.

    This avoids two potential pitfalls with the other answers:

    • Regexes using [A-Z] will fail to match accented and other similar non-A-Z capitalised letters such as in Åland (Scandinavian islands) and Łukasz (common Polish name), including capital letters in non-latin scripts such as Cyrillic or Greek (e.g. Александра).

    • The approach using word[0] === word[0].toUpperCase(), will return true on words that start with non-letters, such as 1940s, 17th, 123reg (company name), abbreviations like 2mrw, or some words in some African languages, such as !xūún or ǂǐ-sì. It'll also treat any input from an alphabet that doesn't have capital letters as being capital letters (e.g. 中文).

    Since this arbitrary-input-safe approach is just as simple and no less readable than the alternatives, it's probably better to use this even if you don't anticipate such exceptions.

    Here's a quick test:

    function a(word){
      return word[0] !== word[0].toLowerCase();
    }
    function b(word){
      return word[0] === word[0].toUpperCase();
    }
    function c(word){
      return /^[A-Z]/.test( word );
    }
    function test(word, answer){
      console.log( 'Should be '+answer+':',  a(word), b(word), c(word), '-------', word );
    }
    
    test( 'Łukasz', true ); // regex test fails, returns false
    test( 'Александра', true ); // regex test fails, returns false
    test( '1940s', false ); // .toUpperCase() test fails, returns true
    test( '中文', false ); // .toUpperCase() test fails, returns true
    
    test( 'ß', false ); // All pass on German "sharp S" that has no uppercase
    test( 'Z̢̜̘͇̹̭a͎͚͔͕̩̬̭͈͞l̩̱̼̤̣g̲̪̱̼̘̜͟ợ̮̱͎̗̕ͅͅ', true ); // All pass. Phew, Zalgo not awakened 

    0 讨论(0)
  • 2020-12-07 23:04

    Typescript

    //userEntry input as string
    //return boolean
    oneUppercaseRequired(userEntry: string): boolean {
       const letters = /[A-Z]/;
    
       if (letters.test(userEntry[0]))
         return true;
    
       return false;
    }
    

    for-loop for other languages Change the syntax to your language!

    //userEntry param as string
    //return boolean
    oneUppercaseRequired(userEntry: string): boolean {
       const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
       for (let i = 0; i < letters.length; i++) {
         if (userEntry[0] == letters[i])
           return true;
       }
    
       return false;
    }
    
    0 讨论(0)
提交回复
热议问题