Split string into words with whitespace unless in between a pair of double quotation marks
A solution :
var str = 'get "something" from "any site"';
var tokens = [].concat.apply([], str.split('"').map(function(v,i){
return i%2 ? v : v.split(' ')
})).filter(Boolean);
Result :
["get", "something", "from", "any site"]
It's probably possible to do simpler. The idea here is to split using "
and then split by the space the odd results of the first splitting.
If you want to keep the quotes, you may use
var tokens = [].concat.apply([], str.split('"').map(function(v,i){
return i%2 ? '"'+v+'"' : v.split(' ')
})).filter(Boolean);
Result :
['get', '"something"', 'from', '"any site"']
Here is how to do it with a regular expression:
("\[a-zA-Z\s\]+"|\[a-zA-Z\]+)/g
: Explanation of the expression is at the link.
Here is how you would use it:
var re = /([a-zA-Z]+)|("[a-zA-Z\s]+"?)\s?/g;
var str = 'get "something" from "any site"';
var match = re.exec(str);
alert(match[1]); \\ this will give you the first matched group
\\ in this case it would be the word "get"