Created
October 12, 2014 13:44
-
-
Save BenjaminKobjolke/f069bd21d2b04b3d2006 to your computer and use it in GitHub Desktop.
Split into Arguments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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