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

120
Visualizações
How to accept any order of groups in regex?

as already mentioned in my provider question, how can I change an regex with names capture groups from a dedicated order of the groups to any order is accepted?

As an example of my data:

{
  att1 = "demo1"
  att2 = "ohno"
}
{
  att2 = "demo1"
  att1 = "ohno"
}

If I use this regex, it will work for the first record but not for the second one

/\{[\n\s]*(?:att1 = \"(?<att1>\w*)\"[\n\s]*(?:att2 = \"(?<att2>\w*)\"[\n\s]*\}/gm

Connecting both groups with an | will not help. Instead it finds only the last group, but why?

/\{[\n\s]*(?:(?:att1 = \"(?<att1>\w*)\"[\n\s]*)|(?:(?:att2 = \"(?<att2>\w*)\"[\n\s]*)\}/gm

Test in online Tools do not really help because there the regex might work bot not in my local nodejs 16 version

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

0

You can use this regex:

/(?<=\{[^}]*?)(?:att1 = "(?<att1>\w*)"|att2 = "(?<att2>\w*)")(?=[^}]*?\})/g

Explanation:

(?<=\{[^}]*?) - look behind for { and zero or more character not being }, as few as possible.

(?: - create a non capturing group with an alternation

att1 = " - match att1 = " literally

(?<att1>\w*) - match and capture zero or more word characters.

"|att2 = " - match " OR àtt2 = "` literally

(?<att2>\w*)" - match and capture zero or more word characters and "

) - end group

(?=[^}]*?\}) - look ahead for zero or more characters not being } as few as possible and the final }

It must have the global flag.

Now it doesn't care about the order, but gets both.

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