I currently have this regular expression to split strings by all whitespace, unless it\'s in a quoted segment:
keywords = \'pop rock \"hard rock\"\';
keyword
ES6 solution supporting:
Code:
keywords.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:
[ 'pop', 'rock', 'hard rock', '"dream" pop' ]