Reputation: 38
I would like to get some parts as group in my string using regex. I tried many solutions but they did not work.
Part of my whole string:
...
{"name":"AE102-Fundamentals of Automotive Engineering","short":"AE102","color":"#00C0FF","picture":"",
"timeoff":[
[
["1"]]],"id":"-696","picture_url":""},
{"name":"AE202 lab-Fuels and Combustion lab","short":"AE202 lab","color":"#FF5050","picture":"",
"timeoff":[
[
["1"]]],"id":"-697","picture_url":""},
{"name":"AE202-Fuels and Combustion","short":"AE202","color":"#CCFFFF","picture":"",
"timeoff":[
[
["1"]]],"id":"-698","picture_url":""},
...
Output should be like:
[
{"name":"...","short":"...","id":"..."},
{"name":"...","short":"...","id":"..."},
....
]
Additionally, The platform is node js.
Upvotes: 0
Views: 35
Reputation: 4097
Don't use a regular expression for this - parse the JSON with JSON.parse
instead, and use .map
to extract the desired properties from each row:
function parse() {
const obj = JSON.parse(input);
const newRows = obj.map(({ name, short, id }) => ({ name, short, id }));
console.log(newRows);
}
const input = `[{
"name": "AE102-Fundamentals of Automotive Engineering",
"short": "AE102",
"color": "#00C0FF",
"picture": "",
"timeoff": [
[
["1"]
]
],
"id": "-696",
"picture_url": ""
},
{
"name": "AE202 lab-Fuels and Combustion lab",
"short": "AE202 lab",
"color": "#FF5050",
"picture": "",
"timeoff": [
[
["1"]
]
],
"id": "-697",
"picture_url": ""
},
{
"name": "AE202-Fuels and Combustion",
"short": "AE202",
"color": "#CCFFFF",
"picture": "",
"timeoff": [
[
["1"]
]
],
"id": "-698",
"picture_url": ""
}
]`;
parse();
Upvotes: 3