javascript 检查字符串是否以小写字母开头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3816905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:21:55  来源:igfitidea点击:

Checking if a string starts with a lowercase letter

javascript

提问by TheStandardRGB

How would I find out if a string starts with a lowercase letter by using an 'if' statement?

如何使用“if”语句确定字符串是否以小写字母开头?

回答by Daniel Vandersluis

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

如果您想涵盖的不仅仅是 az,您可以使用以下内容:

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

回答by kennebec

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
}

回答by lincolnk

This seems like an appropriate use of regular expressions.

这似乎是对正则表达式的适当使用。

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

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