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

308
Visualizações
Regex javascript match the second letter by the first letter, but the second letter must be an uppercase

This regex matches the second letter by the first letter, but the second letter is not an uppercase

([a-z])\1

Now regex matches letters like aa or bb,... but I need that my regex can match all aA,bB,... from this string "abcaAcvbBklmM"

So how to make that regex can match the second value by the first value, but the second must be an uppercase

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

0

You could phrase this by matching all [a-z]\1 in lowercase mode and then checking if each match also matches [a-z][a-Z]:

var input = "abcaAcvbBklmMzzQQ";
var matches = input.match(/([a-z])\1/gi)
                   .filter(x => /^[a-z][A-Z]$/.test(x));
console.log(matches);

about 4 years ago · Juan Pablo Isaza Relatório

0

It's ugly as, but if you must use a simple regex, you could just brute-force the full set of pairings:

var myText = "aAaaABbCCddEEeEfF";
var letterPairs = myText.match(/aA|bB|cC|dD|eE|fF|gG|hH|iI|jJ|kK|lL|mM|nN|oO|pP|qQ|rR|sS|tT|uU|vV|wW|xX|yY|zZ/g);
console.log(letterPairs);

It doesn't feel very satisfactory as a solution, but it'll get the job done.

about 4 years ago · Juan Pablo Isaza Relatório

0

I don't think you can do this with just a JavaScript regular expression (well, not unless you list all possible combinations). Regular expressions in some other environments may be able to do it, but not JavaScript's.

It's fairly straight-forward, though, to match a lower-case character followed by an upper-case character and then filter out ones where the one isn't the lower-case equivalent of the other:

const str = "abcaAcvbBklmM";

const results = [];
for (const [, lower, upper] of str.matchAll(/([a-z])([A-Z])/g)) {
    if (lower === upper.toLocaleLowerCase()) {
        results.push(lower + upper);
    }
}

console.log(results);

How that works:

  • The regular expression /([a-z])([A-Z])/g matches a lower-case letter followed by an upper-case letter (but they aren't necessarily the "same" letter), capturing each in a capture group.
  • matchAll matches all occurrences of that in a string, returning an iterator for the results.
  • The for-of loop loops through the results from the iterator. The results from matchAll are an augmented array,¹ where the first element in the array is the overall match, followed by elements for any capture groups. The code uses destructuring in the for-of to pick out the second and third elements of the array (the two capture groups).
  • If the first letter matches the second letter converted to lower case, it's a match and we retain it.

We can also do this with multiple passes using array methods rather than a single for-of:

const str = "abcaAcvbBklmM";

const results = [...str.matchAll(/([a-z])([A-Z])/g)]
    .filter(([, lower, upper]) => lower === upper.toLocaleLowerCase())
    .map(([match]) => match);

console.log(results);


¹ This code isn't using the augmented parts of the array, but basically it addition to being an array it has an index property saying where the match occurred, a groups property containing capture group information (which we could have used, but it was just as easy to use the array elements for this simple regex), and an input property with the original string.

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