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

126
Visualizações
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 Respostas
Responde à pergunta

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