javascript split string by space, but ignore space in quotes (notice not to split by the colon too)

I need help splitting a string in javascript by space (" "), ignoring space inside quotes expression.

I have this string:

var str = 'Time:"Last 7 Days" Time:"Last 30 Days"';

I would expect my string to be split to 2:

['Time:"Last 7 Days"', 'Time:"Last 30 Days"']

but my code splits to 4:

['Time:', '"Last 7 Days"', 'Time:', '"Last 30 Days"']

this is my code:

str.match(/(".*?"|[^"\s]+)(?=\s*|\s*$)/g);

Thanks!


Solution 1:

s = 'Time:"Last 7 Days" Time:"Last 30 Days"'
s.match(/(?:[^\s"]+|"[^"]*")+/g) 

// -> ['Time:"Last 7 Days"', 'Time:"Last 30 Days"']

Explained:

(?:         # non-capturing group
  [^\s"]+   # anything that's not a space or a double-quote
  |         #   or…
  "         # opening double-quote
    [^"]*   # …followed by zero or more chacacters that are not a double-quote
  "         # …closing double-quote
)+          # each match is one or more of the things described in the group

Turns out, to fix your original expression, you just need to add a + on the group:

str.match(/(".*?"|[^"\s]+)+(?=\s*|\s*$)/g)
#                         ^ here.

Solution 2:

ES6 solution supporting:

  • Split by space except for inside quotes
  • Removing quotes but not for backslash escaped quotes
  • Escaped quote become quote

Code:

str.match(/\\?.|^$/g).reduce((p, c) => {
        if(c === '"'){
            p.quote ^= 1;
        }else if(!p.quote && c === ' '){
            p.a.push('');
        }else{
            p.a[p.a.length-1] += c.replace(/\\(.)/,"$1");
        }
        return  p;
    }, {a: ['']}).a

Output:

[ 'Time:Last 7 Days', 'Time:Last 30 Days' ]