Check If First Letter Of Word Is A Capital Letter
Solution 1:
var word = "Someword";
console.log( word[0] === word[0].toUpperCase() );
or
var word = "Someword";
console.log( /[A-Z]/.test( word[0]) );
or
var word = "Someword";
console.log( /^[A-Z]/.test( word) );
See toUpperCase()
and test()
Solution 2:
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:
functioninitialIsCapital( 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:
functiona(word){
return word[0] !== word[0].toLowerCase();
}
functionb(word){
return word[0] === word[0].toUpperCase();
}
functionc(word){
return/^[A-Z]/.test( word );
}
functiontest(word, answer){
console.log( 'Should be '+answer+':', a(word), b(word), c(word), '-------', word );
}
test( 'Łukasz', true ); // regex test fails, returns falsetest( 'Александра', true ); // regex test fails, returns falsetest( '1940s', false ); // .toUpperCase() test fails, returns truetest( '中文', false ); // .toUpperCase() test fails, returns truetest( 'ß', false ); // All pass on German "sharp S" that has no uppercasetest( 'Z̢̜̘͇̹̭a͎͚͔͕̩̬̭͈͞l̩̱̼̤̣g̲̪̱̼̘̜͟ợ̮̱͎̗̕ͅͅ', true ); // All pass. Phew, Zalgo not awakened
Solution 3:
For English letters only:
'A' => 65'Z' => 90
Meaning, every number between [65, 90] is a capital letter:
functionstartsWithCapitalLetter(word) {
return word.charCodeAt(0) >= 65 && word.charCodeAt(0) <= 90;
}
Solution 4:
Yes.
var str = "Hello";
if(str[0].toUpperCase() == str[0])
{
window.alert('First character is upper case.');
}
Solution 5:
You can do it in several ways:
var myWord = "Hello";
// with string functionsif (myWord.charAt(0) === myWord.charAt(0).toUpperCase()) { /* is upper */ }
// or for newer browsers that support array-style access to string charactersif (myWord[0] === myWord[0].toUpperCase()) { /* is upper */ }
// with regex - may not be appropriate for non-English uppercaseif (/^[A-Z]/.test(myWord) { /* is upper */ }
Note that the array-style access to characters like myWord[0]
is an ECMAScript 5 feature and not supported in older browsers, so (for now) I'd probably recommend the .charAt() method.
If you need to do this test a lot you could make a little function:
functionfirstLetterIsUpper(str) {
var f = str.charAt(0); // or str[0] if not supporting older browsersreturn f.toUpperCase() === f;
}
if (firstLetterIsUpper(myWord)) { /* do something */ }
Post a Comment for "Check If First Letter Of Word Is A Capital Letter"