How would I find out if a string starts with a lowercase letter by using an 'if' statement?
A:
See this question : JavaScript - checking for any lowercase letters in a string
Preet Sangha
2010-09-28 20:49:01
Which has the correct answer, although surprisingly it's not the *accepted* answer.
T.J. Crowder
2010-09-28 21:05:39
A:
This seems like an appropriate use of regular expressions.
var match = myString.match(/^[a-z]/);
if (match != null) {
// good match
}
lincolnk
2010-09-28 20:49:43
That's an extremely English-centric view of the problem. Is "à" not lower case?
T.J. Crowder
2010-09-28 21:06:22
you're right, and I think it would work for that case. It would not be suitable for international use. I voted for Daniel's answer since it's more useful than mine.
lincolnk
2010-09-28 21:18:25
+7
A:
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
Daniel Vandersluis
2010-09-28 20:53:35
What's the case where you need the `toLowerCase` check? E.g., where `!= toUpperCase` isn't sufficient?
T.J. Crowder
2010-09-28 20:57:49
I highly recommend that you use `===` and `!==` to avoid type coercion errors. It is a tough habit to start but I think it is very beneficial.
ChaosPandion
2010-09-28 22:11:22
@Chaos that's what happens when I post right before I need to head out. Corrected!
Daniel Vandersluis
2010-09-28 22:51:28
@T.J. I wasnt sure if there was a case of a character that's isn't equivalent to character.toUpperCase() so I used both conditions, but you make a good point.
Daniel Vandersluis
2010-09-28 22:53:11
In this particular example I don't see a potential *type* coercion issue, because the [`charAt`](http://ecma262-5.com/ELS5_HTML.htm#Section_15.5.4.4), [`toLowerCase`](http://ecma262-5.com/ELS5_HTML.htm#Section_15.5.4.16) and [`toUpperCase`](http://ecma262-5.com/ELS5_HTML.htm#Section_15.5.4.18) methods are guaranteed to return always a string value...
CMS
2010-09-29 01:02:21
@ChaosPandion: `toUpperCase` and `toLowerCase` return strings, there's no opportunity for type issues. Using `===` rather than `==` is beneficial in some situations and not in others, it's not a panacea.
T.J. Crowder
2010-09-29 06:48:45
@T.J. - Although technically you are right it is really all about promoting good habits. I mean you make sure there isn't any bugs in your food right?
ChaosPandion
2010-09-29 16:28:37
@T.J., @Chaos, I would call it a "Crockfordian" *Good* Habit ;) , *understanding* how type conversion works IMHO is a lot better than just using `===` everywhere in your code... [see also](http://stackoverflow.com/questions/3735939/jslint-expected-and-instead-saw/3736117#3736117)
CMS
2010-09-29 18:44:59
@T.J., @CMS - To be honest my obsessive use of the strict equality operators came from a deep understanding of type cohesion. I haven't actually read Crockford's book. *(I have read read snippets though as it is kind of hard to avoid.)*
ChaosPandion
2010-09-30 14:29:16
A:
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
}
kennebec
2010-09-28 22:07:12