Skip to content Skip to sidebar Skip to footer

Javascript - Checking For Any Lowercase Letters In A String

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 an

Solution 1:

functionhasLowerCase(str) {
    return str.toUpperCase() != str;
}

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

Solution 2:

also:

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

Solution 3:

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

or

function hasLowerCase(str) {
    for(x=0;x<str.length;x++)
        if(str.charAt(x) >= 'a' && str.charAt(x) <= 'z')
            returntrue;
    returnfalse;
}

Solution 4:

Another solution only match regex to a-z

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

or

functionnameHere(str) {
        return/[a-z]/g.test(str);
    }

Post a Comment for "Javascript - Checking For Any Lowercase Letters In A String"