1

A note about the possible duplicate question

The accepted answer to that question does not work.

It incorrectly reports spaces and other non-letter characters as toUpperCase==true. :-(

But I now see that one of the other answers does provide a successful solution. The correct answer on this previous question is from ciembor. I'll leave this question posted because this accepted answer from Barmar correctly solves the issue.

How can I determine which characters in a string of text are capital letters?

My first try was testing with .toUpperCase, but non-letter characters also return true:

var text="Romeo & Juliet";

var characters=text.split('');

// and test with 

characters[i]===characters[i].toUpperCase()   // but spaces and "&" also test as true

Next I though of using regex and testing with A-Z but non-English character sets might have capital letters outside this range.

Anyone have way to determine if a character is a capital letter?

markE
  • 102,905
  • 11
  • 164
  • 176
  • Well, there's [this answer](http://stackoverflow.com/a/4052294/283366) however I don't think JavaScript (ES5) supports unicode expressions. See http://stackoverflow.com/questions/280712/javascript-unicode-regexes – Phil May 23 '14 at 04:10
  • FYI, you don't need to split a string to access each character. Strings support array-like access – Phil May 23 '14 at 04:15
  • @Se0ng11. Thanks for the reference to that question. The accepted answer to that question fails to correctly test for spaces, ampersands, etc. But I now see that one of the other answers does provide a successful solution. I'll leave this question posted as the accepted answer from Barmar correctly solves the issue. Again...thanks! – markE May 23 '14 at 04:37

1 Answers1

5

Try this:

function isUpperCase(c) {
    return c == c.toUpperCase() && c != c.toLowerCase();
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Of course--eliminate false positives with !=c.toLowerCase. Thanks...This is what I needed. – markE May 23 '14 at 04:18