Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to use regex after already using regex once

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading