-1

I've got array like this with few values : name, number ( int or float) and %. This is how it looks :

["Ingredients", " Corn starch 33.02%", " sugar 22.21%", " sea salt 20.27% [1]", " peas 10%"]

now in few steps i need to remove [*] so I've got this part :

for (var i = 1; i < lines .length; i++) {
         array1.push(lines [i].replace(/\[.*?\]/g, ''));

And now its part that i complete don't know how to split them into 3 values, my expected output for first value is :

1 : corn starch , 33.02 , %

Could You help me find regex to split it ?

T J
  • 41,966
  • 13
  • 81
  • 134

1 Answers1

0

I made the following:

const lines = ["Ingredients", " Corn starch 33.02%", " sugar 22.21%", " sea salt 20.27% [1]", " peas 10%"];
const results = [];
lines.forEach(line => {
  const filtered = line.split(/([\s\w]+)\s(\d+(?:\.\d+)?)(%?)\s?(?:\[\d\])?/g).filter(Boolean);
  results.push(filtered);
});

console.log(results)

I'm not a regex expert so surely there might be things I missed, but this seems to work with the given input. You can play around with it here:regexr

T J
  • 41,966
  • 13
  • 81
  • 134