65

Consider a JavaScript method that needs to check whether a given string is in all uppercase letters. The input strings are people's names.

The current algorithm is to check for any lowercase letters.

var check1 = "Jack Spratt";    
var check2 = "BARBARA FOO-BAR"; 
var check3 = "JASON D'WIDGET";  

var isUpper1 = HasLowercaseCharacters(check1);  
var isUpper2 = HasLowercaseCharacters(check2);
var isUpper3 = HasLowercaseCharacters(check3);

function HasLowercaseCharacters(string input)
{
    //pattern for finding whether any lowercase alpha characters exist
    var allLowercase; 

    return allLowercase.test(input);
}

Is a regex the best way to go here?

What pattern would you use to determine whether a string has any lower case alpha characters?

p.campbell
  • 98,673
  • 67
  • 256
  • 322

4 Answers4

168

function hasLowerCase(str) {
    return str.toUpperCase() != str;
}

console.log("HeLLO: ", hasLowerCase("HeLLO"));
console.log("HELLO: ", hasLowerCase("HELLO"));
Kiran Dash
  • 4,816
  • 12
  • 53
  • 84
karim79
  • 339,989
  • 67
  • 413
  • 406
68

also:

function hasLowerCase(str) {
    return (/[a-z]/.test(str));
}
ariel
  • 15,620
  • 12
  • 61
  • 73
  • 37
    That's a very English-centric view of what a lowercase letter is. Is "à" not lower case? I'm mentioning this so long after this answer was accepted because this question has been referenced by [this newer question](http://stackoverflow.com/questions/3816905). The `!= toUpperCase` answer below is much more inclusive. – T.J. Crowder Sep 28 '10 at 21:05
  • 2
    This won't work for lowercase, you need to check for `undefined` as well. ` function hasLowerCase(str) { return str ? (/[a-z]/.test(str)) : false; }` – parsh Jun 20 '14 at 21:25
  • Won't work for chars with diacritics. Fot that use `/\p{Ll}/u.test(value)`. – Martin Ždila Mar 11 '21 at 19:08
  • what if you want to find out like 2 lowercase char on an string ? – Pxaml Jul 03 '21 at 21:56
7
function hasLowerCase(str) {
    return str.toUpperCase() != str;
}

or

function hasLowerCase(str) {
    for(x=0;x<str.length;x++)
        if(str.charAt(x) >= 'a' && str.charAt(x) <= 'z')
            return true;
    return false;
}
John ClearZ
  • 952
  • 2
  • 9
  • 16
  • 1
    The second one works with any language that does not have Diacritics or special chars, which is why the first one is prefered. – WilomGfx Apr 02 '17 at 21:32
1

Another solution only match regex to a-z

function nameHere(str) {
    return str.match(/[a-z]/);
}

or

 function nameHere(str) {
        return /[a-z]/g.test(str);
    }
Community
  • 1
  • 1
Ratan Paul
  • 484
  • 9
  • 25
  • 4
    Welcome to StackOverflow. At StackOverflow, it is expected that you read everyone else's answers before posting your own answer. Both of your solutions work, but they are nearly word-word duplicates of Ariel's answer at the top of this page. While you may have come up with your solution on your own, you should never post your solution an answer if someone else has already beaten you to it. P.S. There isn't really any "or." Both of your solutions function identical in all cases, except the former solution is better because it takes up one less character (in the minified file). – Jack G Jun 14 '18 at 16:23
  • this answer could be improved by discussing the difference between `.test` and `.match`, and when to use each one, or at least referencing an answer that does, like this one: https://stackoverflow.com/questions/10940137/regex-test-v-s-string-match-to-know-if-a-string-matches-a-regular-expression/10940138#10940138 – phoenixdown Sep 01 '21 at 23:35