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

264
Vistas
string to dictionary in javascript

I have a string that I want to transform into a dictionary, the string looks like this:

const str = '::student{name="this is the name" age="21" faculty="some faculty"}'

I want to transform that string into a dictionary that looks like this:

const dic = {
  "name": "this is the name",
  "age": "21",
  "faculty": "some faculty"
}

So the string is formated in this was ::name{parameters...} and can have any parameters, not only name, faculty, ... How can I format any string that looks like that and transform it into a dictionary?

Also Is there a way to check that the current string I'm parsing follow this structure ::name{parameters...}, that way I can throw an error when it does not.

Any help would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Assuming that you only have alphanumeric and spaces or an empty string within the parentheses of the values, and there is never a space between the key and value in key="value" you can match with the following regular expression and then iterate over it to construct your desired object.

const str = '::student{name="this is the name" age="21" faculty="some faculty"}'
const matches = str.match(/[\w]+\=\"[\w\s]*(?=")/g)
const result = {}
matches.forEach(match => {
  const [key, value] = match.split('="')
  result[key] = value
})
console.log(result)

The regex is composed of the following parts:

enter image description here

You can use https://regexr.com to experiment with your regular expressions. Depending on the string to process, you'll need to refine your regex.

about 4 years ago · Juan Pablo Isaza Denunciar

0

This example uses exec to look for matches in the string using a regular expression.

const str = '::student{name="this is the name" age="21" faculty="some faculty"}';

const regex = /([a-z]+)="([a-z0-9 ?]+)"/g;

let match, output = {};

while (match = regex.exec(str)) {
  output[match[1]] = match[2];
}

console.log(output);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use 2 patterns. The first pattern to match the format of the string and capture the content between the curly braces in a single capture group. The second pattern to get the key value pairs using 2 capture groups.

For the full match you can use

::\w+{([^{}]*)}
  • ::\w+ match :: and 1+ word characters
  • { Match the opening curly
  • ([^{}]*) Capture group 1, match from opening till closing curly
  • } Match the closing curly

Regex demo

For the keys and values you can use

(\w+)="([^"]+)"
  • (\w+) Capture group 1, match 1+ word chars
  • =" Match literally
  • ([^"]+) Capture group 2, match from an opening till closing double quote
  • " Match closing double quote

Regex demo

const str = '::student{name="this is the name" age="21" faculty="some faculty"}';
const regexFullMatch = /::\w+{([^{}]*)}/;
const regexKeyValue = /(\w+)="([^"]+)"/g;
const m = str.match(regexFullMatch);

if (m) {
  dic = Object.fromEntries(
    Array.from(m[1].matchAll(regexKeyValue), v => [v[1], v[2]])
  );
  console.log(dic)
}

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