Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda