split whitespace except in quotes javascript

Solutions on MaxInterview for split whitespace except in quotes javascript by the best coders in the world

showing results for - "split whitespace except in quotes javascript"
Gail
07 Oct 2016
1// Hard to read, but works
2// If you want to keep the \ in the string, replace the: c.replace(/\\(.)/,"$1"); by: c;
3
4keywords.match(/\\?.|^$/g).reduce((p, c) => {
5        if(c === '"'){
6            p.quote ^= 1;
7        }else if(!p.quote && c === ' '){
8            p.a.push('');
9        }else{
10            p.a[p.a.length-1] += c.replace(/\\(.)/,"$1");
11        }
12        return  p;
13    }, {a: ['']}).a
14