开发者

Checking if a string starts with a lowercase letter

How would I fi开发者_开发技巧nd out if a string starts with a lowercase letter by using an 'if' statement?


If you want to cover more than a-z, you can use something like:

var first = string.charAt(0);
if (first === first.toLowerCase() && first !== first.toUpperCase())
{
  // first character is a lowercase letter
}

Both checks are needed because there are characters (such as numbers) which are neither uppercase or lowercase. For example:

"1" === "1".toLowerCase() //=> true
"1" === "1".toLowerCase() && "1" !== "1".toUpperCase() //=> true && false => false
"é" === "é".toLowerCase() && "é" !== "é".toUpperCase() //=> true && true => true


seems like if a character is not equal to it's upper case state it is lower case.

var first = string.charAt(0);
if(first!=first.toUpperCase()){
    first character is lower case
}


This seems like an appropriate use of regular expressions.

var match = myString.match(/^[a-z]/);

if (match != null) {
    // good match
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜