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

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

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 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