Is it possible in Javascript to find out if the first letter of a word is a capital letter?
是否有可能在Javascript中找出单词的第一个字母是否是大写字母?
7 个解决方案
#1
79
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()
请参阅toUpperCase()和test()
#2
16
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 "Александра".
此页面上的其他答案适用于已知仅包含非重音A-Z字母的字符串。如果你不能保证这一点(例如用户输入),它们可能会产生意想不到的结果:对于像“1940年代”或“中文”这样的不可开头的首字母的误报,或者对于“Łukasz”或“Łukasz”或“罗马”等首字母缩写的假阴性Александра”。
This variant returns true if the initial is any capital letter, and only if it's a capital letter:
如果首字母是任何大写字母,并且仅当它是大写字母时,此变量返回true:
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.
如果需要IE8支持,请使用.charAt(0)而不是[0]。哪个浏览器的速度更快。
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. Александра).使用[AZ]的正则表达式将无法匹配重音和其他类似的非AZ大写字母,例如Åland(斯堪的纳维亚群岛)和Łukasz(常见的波兰语名称),包括西里尔语或希腊语等非拉丁文字母(例如Александра) )。
-
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. 中文).使用word [0] === word [0] .toUpperCase()的方法将在以非字母开头的单词上返回true,例如1940s,17th,123reg(公司名称),缩写如2mrw或某些单词在某些非洲语言中,例如!xūún或ǂǐ-sì。它还会将字母表中没有大写字母的任何输入视为大写字母(例如中文)。
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
#3
8
Yes.
是。
var str = "Hello";
if(str[0].toUpperCase() == str[0])
{
window.alert('First character is upper case.');
}
#4
3
You can do it in several ways:
您可以通过以下几种方式实现:
var myWord = "Hello";
// with string functions
if (myWord.charAt(0) === myWord.charAt(0).toUpperCase()) { /* is upper */ }
// or for newer browsers that support array-style access to string characters
if (myWord[0] === myWord[0].toUpperCase()) { /* is upper */ }
// with regex - may not be appropriate for non-English uppercase
if (/^[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.
请注意,对myWord [0]等字符的数组样式访问是ECMAScript 5的一项功能,在旧版浏览器中不受支持,所以(现在)我可能会推荐使用.charAt()方法。
If you need to do this test a lot you could make a little function:
如果你需要做很多测试,你可以做一点功能:
function firstLetterIsUpper(str) {
var f = str.charAt(0); // or str[0] if not supporting older browsers
return f.toUpperCase() === f;
}
if (firstLetterIsUpper(myWord)) { /* do something */ }
#5
3
For English letters only:
仅限英文字母:
'A' => 65
'Z' => 90
Meaning, every number between [65, 90] is a capital letter:
意思是,[65,90]之间的每个数字都是大写字母:
function startsWithCapitalLetter(word) {
return word.charCodeAt(0) >= 65 && word.charCodeAt(0) <= 90;
}
#6
0
var string1 = "this is a string";
var string2 = "This is a string";
if(string1[0] == string1[0].toUpperCase())
alert('is upper case');
else
alert('is not upper case');
if(string2[0] == string2[0].toUpperCase())
alert('is upper case');
else
alert('is not upper case');
#7
-1
let word = 'Someword';
console.log(word.match(new RegExp(/^[A-Z]/)) !== null);
#1
79
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()
请参阅toUpperCase()和test()
#2
16
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 "Александра".
此页面上的其他答案适用于已知仅包含非重音A-Z字母的字符串。如果你不能保证这一点(例如用户输入),它们可能会产生意想不到的结果:对于像“1940年代”或“中文”这样的不可开头的首字母的误报,或者对于“Łukasz”或“Łukasz”或“罗马”等首字母缩写的假阴性Александра”。
This variant returns true if the initial is any capital letter, and only if it's a capital letter:
如果首字母是任何大写字母,并且仅当它是大写字母时,此变量返回true:
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.
如果需要IE8支持,请使用.charAt(0)而不是[0]。哪个浏览器的速度更快。
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. Александра).使用[AZ]的正则表达式将无法匹配重音和其他类似的非AZ大写字母,例如Åland(斯堪的纳维亚群岛)和Łukasz(常见的波兰语名称),包括西里尔语或希腊语等非拉丁文字母(例如Александра) )。
-
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. 中文).使用word [0] === word [0] .toUpperCase()的方法将在以非字母开头的单词上返回true,例如1940s,17th,123reg(公司名称),缩写如2mrw或某些单词在某些非洲语言中,例如!xūún或ǂǐ-sì。它还会将字母表中没有大写字母的任何输入视为大写字母(例如中文)。
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
#3
8
Yes.
是。
var str = "Hello";
if(str[0].toUpperCase() == str[0])
{
window.alert('First character is upper case.');
}
#4
3
You can do it in several ways:
您可以通过以下几种方式实现:
var myWord = "Hello";
// with string functions
if (myWord.charAt(0) === myWord.charAt(0).toUpperCase()) { /* is upper */ }
// or for newer browsers that support array-style access to string characters
if (myWord[0] === myWord[0].toUpperCase()) { /* is upper */ }
// with regex - may not be appropriate for non-English uppercase
if (/^[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.
请注意,对myWord [0]等字符的数组样式访问是ECMAScript 5的一项功能,在旧版浏览器中不受支持,所以(现在)我可能会推荐使用.charAt()方法。
If you need to do this test a lot you could make a little function:
如果你需要做很多测试,你可以做一点功能:
function firstLetterIsUpper(str) {
var f = str.charAt(0); // or str[0] if not supporting older browsers
return f.toUpperCase() === f;
}
if (firstLetterIsUpper(myWord)) { /* do something */ }
#5
3
For English letters only:
仅限英文字母:
'A' => 65
'Z' => 90
Meaning, every number between [65, 90] is a capital letter:
意思是,[65,90]之间的每个数字都是大写字母:
function startsWithCapitalLetter(word) {
return word.charCodeAt(0) >= 65 && word.charCodeAt(0) <= 90;
}
#6
0
var string1 = "this is a string";
var string2 = "This is a string";
if(string1[0] == string1[0].toUpperCase())
alert('is upper case');
else
alert('is not upper case');
if(string2[0] == string2[0].toUpperCase())
alert('is upper case');
else
alert('is not upper case');
#7
-1
let word = 'Someword';
console.log(word.match(new RegExp(/^[A-Z]/)) !== null);