-2

Taking input from a user that would be:

!timeout 60 username reason

!timeout, 60, and username will never have spaces, but it can be assumed that reason will usually have spaces.

I'd hope to end up with:

var1 = "!timeout"
var2 = 60
var3 = "username"
var4 = "reason"

1 Answers1

1

For time-saving nirvana, I'd probably use a simple split and some presumptuous array access:

var test = "!timeout 60 username reason and then some";

var chunks = test.split(" ");

var timeout = chunks[0];
var time = chunks[1];
var username = chunks[2];
var reason = chunks.slice(3).join(' ');

console.log(timeout, '|', time, '|', username, '|', reason);

Or a nice little one-liner:

var test = "!timeout 60 username reason and then some";

var result = test.split(" ", 3).concat(test.split(" ").slice(3).join(' '));

console.log(result);
shennan
  • 10,798
  • 5
  • 44
  • 79
  • Why bother with a regex that contains just a space? You can just pass `" "`. – jfriend00 Oct 13 '15 at 00:43
  • @jfriend00 Very true. Probably because I was going to try and find some catch-all regex at first. Will update the answer with just `" "`. – shennan Oct 13 '15 at 00:45
  • Or only split once: `(function(s) {return s.slice(0, 3).concat(s.slice(3).join(" "));})(test.split(" "));` – Jason Oct 13 '15 at 01:00
  • @Jason Yep. Though aesthetically I find it uglier. – shennan Oct 13 '15 at 01:06
  • And your one-liner isn't an eyesore? ;) – Jason Oct 13 '15 at 01:07
  • @Jason It's that little bit prettier without the squigglies. :-) I'm not sure I like the idea of creating and dumping anonymous scopes in the same breath too... But I can see value in the approach. – shennan Oct 13 '15 at 01:09