开发者

Checking if a string starts with a lowercase letter

开发者 https://www.devze.com 2023-01-18 12:25 出处:网络
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:

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

精彩评论

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