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

124
Vistas
js and regex exclude {} from string

Trying to exclude the {} from a string such as below with regex, but so far the {} is still in the created array. Please see jsfiddle example (example part of larger code so I understand the code can be simplified). Resultat is:

["{1", "\"FAAS\"}"]

But would like

["1", "\"FAAS\""]

https://jsfiddle.net/173os8w6/

var objectX = {};
  var text = `{1 "FAAS"}`;
  var lines = text.split('\n');
  for (var line = 0; line < lines.length; line++) {
        const regex = /{([^\s"]+)|"([^"]*)"}/g;  
        var row2 = [];                 
        let result = "";
        while(result = regex.exec(lines[line])) {
          row2.push(result[0]); //push parts to array
        } 
        console.log(row2);
  }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to analyze the captures and add the right values accordingly:

while(result = regex.exec(lines[line])) {
    if (result[1]) {
        row2.push(result[1]);
    } else {
        row2.push(result[2]);
    }
} 

Here is your fixed JavaScript demo:

var objectX = {};
var text = `{1 "FAAS"}`;
var lines = text.split('\n');
for (var line = 0; line < lines.length; line++) {
  const regex = /{([^\s"]+)|"([^"]*)"}/g;
  var row2 = [];
  let result = "";
  while (result = regex.exec(lines[line])) {
    if (result[1]) {
      row2.push(result[1]);
    } else {
      row2.push(result[2]);
    }
  }
  console.log(row2);
}

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