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
- ensure you understand my regex (read it step by step)
- test and adapt with the knowledge of your domain
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 parenthesis
Be 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".