Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BenjaminKobjolke/f069bd21d2b04b3d2006 to your computer and use it in GitHub Desktop.
Save BenjaminKobjolke/f069bd21d2b04b3d2006 to your computer and use it in GitHub Desktop.
Split into Arguments
//The parenthesis in the regex creates a captured group within the quotes
var myRegexp = /[^\s"]+|"([^"]*)"/gi;
var myString = 'single words "fixed string of words"';
var myArray = [];
do {
//Each call to exec returns the next regex match as an array
var match = myRegexp.exec(myString);
if (match != null)
{
//Index 1 in the array is the captured group if it exists
//Index 0 is the matched text, which we use if no captured group exists
myArray.push(match[1] ? match[1] : match[0]);
}
} while (match != null);
myArray will now contain exactly what the OP asked for:
single,words,fixed string of words
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment