This is slight variant of a common question: how do you split a string by whitespace, unless that whitespace is contained within a pair of quotes (either " or ')? There are a lot of questions like this here, and the best answer I've found so far is this one. The problem is, all these answer include the quotes themselves in the matches. For instance:
"foo bar 'i went to a bar'".match(/[^\s"']+|"([^"]*)"|'([^']*)'/g);
Results in:
["foo", "bar", "'i went to a bar'"]
Is there a solution that results in:
["foo", "bar", "i went to a bar"]
Note there is an edge case around this:
"foo bar \"'Hi,' she said, 'how are you?'\"".match(...);
=> // ["foo", "bar", "'Hi,' she said, 'how are you?'"]
That is to say, a substring should be able to include quotations of its own, which means that aggressively doing something like this won't work:
"foo bar \"'Hi,' she said, 'how are you?'\"".match(...).map(function(string) {
return string.replace(/'|"/g, '');
});
Update:
We can basically get it working with this:
"foo bar \"'Hi,' she said, 'how are you?'\"".match(/[^\s"']+|"([^"]*)"|'([^']*)'/g).map(function(string) {
return string.replace(/^('|")|('|")$/g, '');
});
But that's quite ugly. (And it will also break an edge case like "5ft 5feet 5'".) There's gotta be a way to shrink that to a single regex, right?