Skip to content

Instantly share code, notes, and snippets.

@yano3nora
Created July 1, 2017 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yano3nora/a8398dd049fae106733b256c37af0f79 to your computer and use it in GitHub Desktop.
Save yano3nora/a8398dd049fae106733b256c37af0f79 to your computer and use it in GitHub Desktop.
[js: command line parse] command line string parse. #js #regex
var cmd = "> create user name \"comment comment\"";
var reg = new RegExp(/\s+(?:"((?:\\"|[^"])*)"?|([^"]\S*))/);
var inputArray = [];
inputArray = cmd.split(reg);
var cmds = [];
for (var i = 0; i < inputArray.length; i++) {
if (inputArray[i]) {
cmds.push(inputArray[i]);
}
}
console.dir(cmds);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment