I have a bot which processes strings with given arguments. Here is what I've tried to get parameters of command:
parse: function (message, argLength) {
var words = message.split(" ");
words.shift(); // Don't return command name in array.
if (words.length < argLength) // If there is not enough parameters, return null
return null;
else if (words.length == argLength) { // If length is exact same, return
return words;
}
else { //Otherwise, concenate first ones till it is exact length.
var concenateString = "";
var length = words.length - argLength + 1;
for (var i = 0; i < length; i++) {
var element = words[0];
concenateString += " " + element;
words.shift();
}
words.unshift(concenateString);
return words;
}
}
"
"
Before doing any business logic you could use a regex to extract anything between "
or words:
var str = 'one two "three is family"'
var re = /"([^"]+)"|([a-zA-Z0-9]+)/g
console.log(
str.match( re )
)