Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - RegEx Uppercase and LowerCase and Mixed

I have this code:

var cadena = prompt("Cadena:");

document.write(mayusminus(cadena));

function mayusminus(cad){
    var resultado = "Desconocido";

    if(cad.match(new RegExp("[A-Z]"))){
        resultado="mayúsculas";
    }else{
        if(cad.match(new RegExp("[a-z]"))){
            resultado="minúsculas";
        }else{
            if(cad.match(new RegExp("[a-zA-z]"))){
            resultado = "minúsculas y MAYUSCULAS";
            }
        }
    }
    return resultado;
}

I always have mayusculas or minusculas, never minusculas y MAYUSCULAS (MIXED), I am learning regexp and dont know my error yet :S

like image 297
Genaut Avatar asked Dec 21 '22 15:12

Genaut


1 Answers

new RegExp("[A-Z]")

matches when any character in cadena is an upper-case letter. To match when all characters are upper-case, use

new RegExp("^[A-Z]+$")

The ^ forces it to start at the start, the $ forces it to end at the end and the + ensures that between the end there are one or more of [A-Z].

like image 198
Mike Samuel Avatar answered Dec 28 '22 23:12

Mike Samuel