-2

I have the following code and I've got the id and name value in its own square bracket as shown below. How can I access the value of id and name?

let data = "[[id=3, name=BOX-3, description=test, comment=test, locationId=5, capacity=null]];"

        let id = data.match(/(?<=id=)\d+(?=\,)/g);
        let name = data.match(/(?<=name=)[\w|-]*/g);

console.log(data);
console.log(id);
console.log(name);


console.log(id.match(/(?<=\[)[^\][]*(?=])/g) );

//console.log(idVal);
Tan
  • 1,433
  • 5
  • 27
  • 47

1 Answers1

-1

You can destructure the result to get the match.

let data = "[[id=3, name=BOX-3, description=test, comment=test, locationId=5, capacity=null]];"
let [id] = data.match(/(?<=id=)\d+(?=\,)/g);
let [name] = data.match(/(?<=name=)[\w|-]*/g);

console.log(id);
console.log(name);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80