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

252
Visualizações
Why is following regular expression as greedy?

Why does (.*?) group in /({\/\* )?#if (.*?)( \*\/})?/ not capture process.env.MODE === "std" when using {/* #if process.env.MODE === "std" */} as input?

I am aware removing ? from last group works… that said, it is a requirement.

Is it possible to work around this?

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

0

If you need to have an optional pattern at the end after an optional pattern (like .*?), you can convert the .*? lazy dot pattern into a tempered greedy token:

({\/\* )?#if ((?:(?! \*\/}).)*)( \*\/})?

See the regex demo.

The .*? is changed into a pattern that matches any char, zero or more but as many as possible occurrences, that is not a starting point of the */} char sequence.

Details:

  • ({\/\* )? - an optional Group 1: /* and space
  • #if - an #if string
  • ((?:(?! \*\/}).)*) - Group 2: any char other than line break chars, zero or more but as many as possible occurrences, that is not a starting point of the */} char sequence
  • ( \*\/})? - an optional Group 3: */} string
about 4 years ago · Juan Pablo Isaza Relatório

0

remove the optional '?' from the end of the regex which is making the last group optional

it should be

({\/\* )?#if (.*?)( \*\/})
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