Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
How could I detect if there is an uppercase character in a string

I've read countless articles about people able to detect if all characters in a string are uppercase...

function isUpperCase(str) {
    return str === str.toUpperCase();
}


isUpperCase("hello"); // false
isUpperCase("Hello"); // false
isUpperCase("HELLO"); // true
But I'm curious how I can take a string, and search to see if any characters are uppercase, and if so, return true or return a string with the characters that are uppercase.

Any help is massively appreciated, I'm trying to avoid posts on here but I couldn't find an answer that worked for me anywhere else.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use the same logic you used for all uppercase, turn the string to lowercase and if its not equal to the original string it has an uppercase letter in it.

function hasUpperCase(str) {
    return str !== str.toLowerCase();
}


console.log(hasUpperCase("hello")); // false
console.log(hasUpperCase("Hello")); // true
console.log(hasUpperCase("HELLO")); // true

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a regular expression to filter out and return uppercase characters. However, note that the naïve solution (as proposed by some of the other answers), /[A-Z]/, would detect only 26 uppercase Latin letters.

Here is a Unicode-aware regex solution:

const isAnyUpper = string => /\p{Lu}/u.test(string)

isAnyUpper('a') //    false
isAnyUpper('A') //    true
isAnyUpper('ф') //    false
isAnyUpper('á') //    false
isAnyUpper('Á') //    true
about 4 years ago · Juan Pablo Isaza Relatório

0

Assuming the function is only taking string of ascii letters:

function isAllUpper(str) {
    return str.split('').findIndex(ch =>  {
      const code = ch.charCodeAt(0);
      return code >= 65 && code <= 90;
    }) === -1;
}

If you want to return just the string with only the uppercase letters, then you can extend the above slightly:

function onlyUpper(str) {
    return str.split('').filter(ch =>  {
      const code = ch.charCodeAt(0);
      return code >= 65 && code <= 90;
    }).join('');
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda