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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.