66

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?

2
  • Just a-z. Not à or à for example? Commented May 13, 2010 at 22:57
  • The title says "any lowercase", the introduction says "all uppercase". That's not the same. Decide for one and then adjust the function and variable names. Commented Apr 9, 2018 at 18:13

4 Answers 4

169

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

console.log("HeLLO: ", hasLowerCase("HeLLO"));
console.log("HELLO: ", hasLowerCase("HELLO"));

4
  • 8
    it would fail, if string is all digits or non-letters Commented Jul 2, 2017 at 16:59
  • 9
    @DipenduPaul No, the function would not fail. It would return false, which is correct, since an all-digit string does not have any lowercase characters. Commented Apr 9, 2018 at 18:08
  • 6
    Why != instead of the type-safe !==? Commented Apr 9, 2018 at 18:09
  • Nice. Nowadays it would be: const hasLowerCaseLetter = (str) => str.toUpperCase() !== str; Commented Aug 27, 2022 at 8:31
69

also:

function hasLowerCase(str) {
    return (/[a-z]/.test(str));
}
4
  • 38
    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. The != toUpperCase answer below is much more inclusive. Commented Sep 28, 2010 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
    Commented Jun 20, 2014 at 21:25
  • 1
    Won't work for chars with diacritics. Fot that use /\p{Ll}/u.test(value). Commented Mar 11, 2021 at 19:08
  • what if you want to find out like 2 lowercase char on an string ?
    – Pxaml
    Commented Jul 3, 2021 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;
}
1
  • 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
    Commented Apr 2, 2017 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);
    }
2
  • 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
    Commented Jun 14, 2018 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: stackoverflow.com/questions/10940137/… Commented Sep 1, 2021 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.