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

170
Visualizações
Regex matching key value pairs inside curly brackets

I’m trying to capture key:value pairs inside curly brackets. For the following string it should return something like k2:v2 and k3:v3

Str = “k1:v1abc{k2:v2,k3:v3}{test}”

I have the following regexes which capture what I need individually, but I don’t know how to combine them together.

/(\w+):(\w+)/g >>> Captures key:value /{.*?}/g >>> Captures everything between {}

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

0

If your environment supports a quantifier in a lookbehind assertion:

(?<={[\w:,]*)\w+:\w+(?=[\w:,]*})

The pattern matches:

  • (?<={[\w:,]*) Positive lookbehind, assert { and optional repetitions of the allowed chars to the left
  • \w+:\w+ Match 1+ word chars : and 1+ word chars
  • (?=[\w:,]*}) Positive lookahead, assert optional repetitions of the allowed chars to the right and }

See a regex demo.

const str = "k1:v1abc{k2:v2,k3:v3}{test}";
const regex = /(?<={[\w:,]*)\w+:\w+(?=[\w:,]*})/g;
console.log(str.match(regex));

Another way could be using 2 passes, first matching the format and get the part between the curly braces in a capture group.

If there is a match, split group 1 on a comma.

{(\w+:\w+(?:,\w+:\w+)*)}

The pattern matches:

  • { Match opening {
  • ( Capture group 1
    • \w+:\w+ Match 1+ word chars : and 1+ word chars
    • (?:,\w+:\w+)* Optionally repeat a , and the same previous pattern
  • )
  • } Match closing }

const str = "k1:v1abc{k2:v2,k3:v3}{test}";
const regex = /{(\w+:\w+(?:,\w+:\w+)*)}/;
const m = str.match(regex);
if (m) console.log(m[1].split(","));

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