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

140
Visualizações
Regex to catch all cases

I have the following stringified javascript-snippet that I would like to capture each case using regex

// case 1
let something = 1
something++

// case 2
const somethingElse = 2
const x = somethingElse + 10 // whatever

// case 3
enter code here

Cases keep going infinitely.. so I am looking for a regex that captures each case with all belonging code up until the next case is reached.

example of the first match should be

// case 1
let something = 1
something++

The best I could get is the following, but it always disregard the last case

([\/]{2}[\s]*?case[\s\S]+?(?=[\/]{2}))

Obviously, the Lookahead is the reason, I had other forms of regex but couldn't get a bulletproof regex..

Note: all cases should be identified consistently. The example above is using a simple pattern case [digit]

Any help is appreciated!

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

0

You can use a simple alternation in your look ahead:

/([\/]{2}[\s]*?case[\s\S]+?(?=[\/]{2}|$))/gi

I just added |$ in the look ahead, which means: OR end of text

So it will look ahead for / or end of text.

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