Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!