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

255
Visualizações
Regex: Match every character between two strings

I am trying to find a way to match every character between two strings.

For example, for the given string abc--def--ghi, I want the regex to match d, e, and f.

I've tried using the following regex (?<=--)(.*)(?=--), however this matches all the characters between -- (def), whereas I need to match every character between --.

s.match(/--(.*?)--/)[1].split("") doesn't work as I need to do this without splitting.

Any suggestions?

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

0

In JavaScript, using the ECMAScript 2018+ compliant regex engine, you can achieve what you want without additional split step using

/(?<=--(?:(?!--).)*).(?=(?:(?!--).)*--)/gs
/(?<=--[^-]*(?:-(?!-)[^-]*)*).(?=[^-]*(?:-(?!-)[^-]*)*--)/gs

See the regex demo (the second variant is the same regex as the first one, but more efficient as it follows the "unroll-the-loop" principle). Details:

  • (?<=--(?:(?!--).)*) - a location immediately preceded by -- and then any one or more (as many as possible) chars, each of which does not start a -- char sequence
  • . - any single char
  • (?=(?:(?!--).)*--) - immediately followed by any one or more (as many as possible) chars, each of which does not start a -- char sequence, and then --.

The s flag enables . to match any char including line break chars that . does not match by default.

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