var message = "<Eci_0007> Dude where's my car?</Eci_0007>\nThis is some more text."
var commandFull = "Text(Print This Info)+Text( Add more to the string)+Eci(0007).Pos(0,8).Trim(trim_leading)+Text( Dont' dropt + that dundnanda)+Text( another test).Trim(trim_leading)";
function commandParse(message, command){
var charList = [];
var grammerList = [];
var outputList = [];
var tempOutput = "";
for(var i = 0; i < command.length; i++){
var grammerJoin = grammerList.join("");
if(command[i] == '('){
continue;
}
if(grammerJoin == 'Text' ||
grammerJoin == 'Eci' ||
grammerJoin == 'Pos' ||
grammerJoin == 'Trim' ||
grammerJoin == '+' ||
grammerJoin == '.'){
if(command[i] == ')'){
switch(grammerJoin){
case "Text":
tempOutput = charList.join("");
outputList.push(tempOutput);
tempOutput = "";
break;
case "Eci":
var searchRegex = "<Eci_"+charList.join("")+">.*<\/Eci_"+charList.join("")+">";
tempOutput = message.match(new RegExp(searchRegex,"gm")).toString();
var beginningBracket = searchRegex.split(".*")[0];
var endingBracket = searchRegex.split(".*")[1];
var replaceBracketRegex = beginningBracket+"|"+endingBracket
tempOutput = tempOutput.replace(new RegExp(replaceBracketRegex,"gm"), "");
outputList.push(tempOutput);
tempOutput = "";
break;
case "Pos":
var subStringVal = charList.join("");
tempOutput = tempOutput.substring(subStringVal.split(",")[0],subStringVal.split(",")[1]);
outputList.push(tempOutput);
tempOutput = "";
break;
case "Trim":
if(charList.join("") == 'trim_leading'){
tempOutput = tempOutput.trimLeft();
}else{
tempOutput = tempOutput.trimRight();
}
outputList.push(tempOutput);
tempOutput = "";
break;
}
while(grammerList.length > 0 ){
grammerList.pop();
}
while(charList.length > 0 ){
charList.pop();
}
}else if(grammerJoin == '+'){
outputList.push(grammerList.pop());
grammerList.push(command[i]);
}else if(grammerJoin == '.'){
tempOutput = outputList.pop();
grammerList.pop();
grammerList.push(command[i]);
}else{
charList.push(command[i]);
}
}else{
grammerList.push(command[i]);
}
}
while(outputList.length > 1){
var endStr = outputList.pop();
if(outputList[outputList.length-1] == '+'){
outputList.pop();
}
var frontStr = outputList.pop();
outputList.push(frontStr+endStr);
}
return outputList.join("");
}
print(commandParse(message, commandFull));