I'm trying to extract some results from a download manager, the format is:
[#8760e4 4.3MiB/40MiB(10%) CN:2 DL:4.9MiB ETA:7s]
what I'd like to extract from the above example, would be an array that looks like this:
['4.3','MiB','40','MiB','10%','4.9','MiB','7','s']
I've tried to split this in various combinations, but nothing seems to be right. Would anyone happen to know how to do this or be able to offer suggestions?
Thank you!
You can do
var arr = str.match(/ ([\d\.]+)(\w+)\/([\d\.]+)(\w+)\(([^\)]+)\).*:([\d\.]+)(\w+).*:([\d\.]+)(\w+)/).slice(1)
With your string, it gives
["4.3", "MiB", "40", "MiB", "10%", "4.9", "MiB", "7", "s"]
but it really depends on the possible strings. With only one example it's impossible to be sure. My advice would be to
Here's an explanation : In between parenthesis, you have capturing groups, that's what we get in the array. Here are some of them :
([\d\.]+) : this group is made of digit(s) and dot(s) (if you want to ensure there's at most one dot, use (\d+\.?\d*))(\w+) : some letters([^\)]+) : some characters that aren't closing parenthesisBe careful that if it gets too complex or deeply structured, then regexes won't be the right solution and you'll have to use a parsing logic.
EDIT
Following your comments, to help you with more complex strings.
Supposing you use this regex :
/ ([\d\.]+)(\w+)\/([\d\.]+)(\w+)\(([^\)]+)\).*:([\d\.]+)(\w+) ETA:(\d+h)?(\d+m)?(\d+s)?/
then
"[#8760e4 4.3MiB/40MiB(10%) CN:2 DL:4.9MiB ETA:1h30m7s]"
would give
["4.3", "MiB", "40", "MiB", "10%", "4.9", "MiB", "1h", "30m", "7s"]
and
"[#8760e4 4.3MiB/40MiB(10%) CN:2 DL:4.9MiB ETA:7s]"
would give
["4.3", "MiB", "40", "MiB", "10%", "4.9", "MiB", undefined, undefined, "7s"]
I changed the end of the regex. A group like (\d+h)? means "some digits followed by h, optionnal".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With