Skip to content

Instantly share code, notes, and snippets.

@FGRibreau
Created July 25, 2012 12:47
Show Gist options
  • Save FGRibreau/3175997 to your computer and use it in GitHub Desktop.
Save FGRibreau/3175997 to your computer and use it in GitHub Desktop.
Extract arguments from a string (quoted or not)
function extractArgs(str){
var args = [], buff = '', idx = -1, insideQuote = false, lgth = str.length;
function next(){return str.charAt(++idx);}
function peak(){return str.charAt(idx+1);}
function resetState(){insideQuote = false;buff = '';}
while(idx < lgth){
var c = next();
if(c === ' ' && !insideQuote){
if(buff.length === 0){continue;}
args.push(buff);
resetState();
continue;
}
if(c === '\\' && insideQuote){
buff += next();
continue;
}
if(c === "'" || c === '"'){
if(!insideQuote){// enter
insideQuote = true;
continue;
} else {// leave
insideQuote = false;
args.push(buff);
resetState();
continue;
}
}
buff += c;
}
if(buff.length > 0){args.push(buff);}
return args;
}
console.log(extractArgs("bonjour ça 'va' \"hello world\" \"c\\\"est ça !\" \"hey hey\""));
// [ 'bonjour', 'ça', 'va', 'hello world', 'c"est ça !', 'hey hey' ]
@WebReflection
Copy link

function args(s) {
  var q = [];
  s = s.replace(/(["'])(?:(?=(\\?))\2.)*?\1/g, function (m) {
    q.push(m.slice(1, -1));
    return "";
  });
  return s.replace(/^\s+|\s+$/g, "").split(/\s+/).concat(q);
}

@WebReflection
Copy link

in case the order is not relevant ... what you wrote makes sense in C, not in JS, imho

@epplestun
Copy link

the faster way is the use of regexp, here the tests http://jsperf.com/extract

@iadvize
Copy link

iadvize commented Jul 25, 2012

for me and with chrome the fastest one is without regexp :)

@FGRibreau
Copy link
Author

In fact, I optimized my JavaScript code for speed ("v2") and as you can see it's faster than regexp http://jsperf.com/extract/4 while still holding the order of values (which was a requirement /cc @WebReflection)

@WebReflection
Copy link

up to 10% faster, and we are talking about 250.000 and more per second, against up to 80% more code ? As I have said, that is a good way in C, not in JS ... but of course if code size is not a concern, many regexps can be replaced by procedural code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment