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

130
Visualizações
split with regexr with no empty elements

I want output be like this:
['YY','RR'] with no empty elements

function pntSq(m){
    let a = m.split(/([A-Z][A-Z])/gi)
    console.log(a)
}
pntSq("YYRR")
//    output :
//    [ '', 'YY', '', 'RR', '' ]

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

0

You can match instead of using split.

function pntSq(m){
  return m.match(/[A-Z]{2}/g);
}

console.log(pntSq('YYRR'));

about 4 years ago · Juan Pablo Isaza Relatório

0

Link: regex101

This matches any 2 uppercase letters together, but note that they may be different.

[A-Z]{2}

To match the same uppercase letter 2 times. This ensures that it's the same letter repeated and not different letters.

([A-Z])\1
about 4 years ago · Juan Pablo Isaza Relatório

0

JavaScript may not support splitting on lookbehinds. One workaround would be to do this in two steps. First we can replace on the following regex pattern:

([A-Z])(?!\1)(?=.)

This will capture any capital which in turn is followed by a different letter (and also not the end of the string). Then, we replace with a single space. As the second step, we simply split on space to get the output array we want.

function pntSq(m) {
    m = m.replace(/([A-Z])(?!\1)(?=.)/g, "$1 ");
    console.log(m);
    var a = m.split(/ /g);
    console.log(a);
}

pntSq("YYRRAAABBBBBCC");

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