4

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

3
  • Please add the call of mayusminus to your question, including its parameters, the expected result and the actual result. Commented Oct 5, 2012 at 12:08
  • you have a mistake in the last [a-zA-z] - it should be [a-zA-Z] in any case (pun intended) Commented Oct 5, 2012 at 12:18
  • Zeta you can read all the post and you see the call to mayusminus, and the expected result and actual result :S Commented Oct 5, 2012 at 14:00

3 Answers 3

6
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].

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

1 Comment

Thanks! I understand regex better now :)
2

I believe you wanted to use regex patterns ^[a-z]+$, ^[A-Z]+$ and ^[a-zA-Z]+$.

In regex, the caret ^ matches the position before the first character in the string. Similarly, $ matches right after the last character in the string. Additionaly, + means one or more occurrences.

It is necessary to use ^ and $ in the pattern, if you want to ensure no other then listed characters are in the string.


JavaScript:

s = 'tEst'; r = (s.match(new RegExp("^[a-z]+$"))) ? 'minúsculas' : (s.match(new RegExp("^[A-Z]+$"))) ? 'mayúsculas' : (s.match(new RegExp("^[a-zA-Z]+$"))) ? 'minúsculas y mayúsculas' : 'desconocido'; 

Test this code here.

1 Comment

Thanks! I understand regex better now :)
2

Let's say cad is foo:

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

Now, let's say cad is FOO:

// will return true if (cad.match(new RegExp("[A-Z]"))) { // so will go there 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"; } } } 

Finally, let's say cad is FoO:

// will return true if (cad.match(new RegExp("[A-Z]"))) { // so will go there 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"; } } } 

As you can see, the nested else is never visited.

What you can do is:

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

Explanation:

^ means from the beginning of the string,

$ means to the end of the string,

<anything>+ means at least one anything.

That said,

^[A-Z]+$ means the string should only contains uppercased chars,

^[a-z]+$ means the string should only contains lowercased chars.

So if the string isn't only composed by uppercased or lowercased chars, the string contains both of them.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.