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"));

Sign up to request clarification or add additional context in comments.

4 Comments

it would fail, if string is all digits or non-letters
@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.
Why != instead of the type-safe !==?
Nice. Nowadays it would be: const hasLowerCaseLetter = (str) => str.toUpperCase() !== str;
69

also:

function hasLowerCase(str) { return (/[a-z]/.test(str)); } 

4 Comments

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.
This won't work for lowercase, you need to check for undefined as well. ` function hasLowerCase(str) { return str ? (/[a-z]/.test(str)) : false; }`
Won't work for chars with diacritics. Fot that use /\p{Ll}/u.test(value).
what if you want to find out like 2 lowercase char on an string ?
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 Comment

The second one works with any language that does not have Diacritics or special chars, which is why the first one is prefered.
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 Comments

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).
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/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.