views:

55

answers:

1
filter -n ""function(file) { return file.owner == "john"; }""

should be parsed into the following array:

[ 'filter',
  '-n',
  'function(file) { return file.owner == "john"; }' ]
+1  A: 

I'm not entirely sure how you want to handle the double quotes. Do you want to also be able to handle strings with only a single double quote on each end, or are the quotes always doubled?

var string = 'filter -n ""function(file) { return file.owner == "john"; }""';
var regex  = /([^"\s]+)|""(.*?)""/g;
var match;
var result = [];

while (match = regex.exec(string)) {
    result.push(match[1] || match[2]);
}

alert(result);

Result:

filter,-n,function(file) { return file.owner == "john"; }
John Kugelman
there will always be 2 double quotes surrounding the string. I guess, if I use 1 double quote around the string, I will need to escape the quotes inside the string
Shahriar
var regex = /([^"\s]+)|""(.*?)""/g, match; // what does match mean here ?
Shahriar
@Shahriar: He's just predefining `match` as a variable. Got lazy and did it all on one line.
Brock Adams