fork download
  1. var message = "<Eci_0007> Dude where's my car?</Eci_0007>\nThis is some more text."
  2. 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)";
  3. function commandParse(message, command){
  4. var charList = [];
  5. var grammerList = [];
  6. var outputList = [];
  7. var tempOutput = "";
  8. for(var i = 0; i < command.length; i++){
  9. var grammerJoin = grammerList.join("");
  10. if(command[i] == '('){
  11. continue;
  12. }
  13. if(grammerJoin == 'Text' ||
  14. grammerJoin == 'Eci' ||
  15. grammerJoin == 'Pos' ||
  16. grammerJoin == 'Trim' ||
  17. grammerJoin == '+' ||
  18. grammerJoin == '.'){
  19. if(command[i] == ')'){
  20. switch(grammerJoin){
  21. case "Text":
  22. tempOutput = charList.join("");
  23. outputList.push(tempOutput);
  24. tempOutput = "";
  25. break;
  26. case "Eci":
  27. var searchRegex = "<Eci_"+charList.join("")+">.*<\/Eci_"+charList.join("")+">";
  28. tempOutput = message.match(new RegExp(searchRegex,"gm")).toString();
  29. var beginningBracket = searchRegex.split(".*")[0];
  30. var endingBracket = searchRegex.split(".*")[1];
  31. var replaceBracketRegex = beginningBracket+"|"+endingBracket
  32. tempOutput = tempOutput.replace(new RegExp(replaceBracketRegex,"gm"), "");
  33. outputList.push(tempOutput);
  34. tempOutput = "";
  35. break;
  36. case "Pos":
  37. var subStringVal = charList.join("");
  38. tempOutput = tempOutput.substring(subStringVal.split(",")[0],subStringVal.split(",")[1]);
  39. outputList.push(tempOutput);
  40. tempOutput = "";
  41. break;
  42. case "Trim":
  43. if(charList.join("") == 'trim_leading'){
  44. tempOutput = tempOutput.trimLeft();
  45. }else{
  46. tempOutput = tempOutput.trimRight();
  47. }
  48. outputList.push(tempOutput);
  49. tempOutput = "";
  50. break;
  51.  
  52. }
  53. while(grammerList.length > 0 ){
  54. grammerList.pop();
  55. }
  56. while(charList.length > 0 ){
  57. charList.pop();
  58. }
  59. }else if(grammerJoin == '+'){
  60. outputList.push(grammerList.pop());
  61. grammerList.push(command[i]);
  62. }else if(grammerJoin == '.'){
  63. tempOutput = outputList.pop();
  64. grammerList.pop();
  65. grammerList.push(command[i]);
  66. }else{
  67. charList.push(command[i]);
  68. }
  69. }else{
  70. grammerList.push(command[i]);
  71. }
  72.  
  73. }
  74. while(outputList.length > 1){
  75. var endStr = outputList.pop();
  76. if(outputList[outputList.length-1] == '+'){
  77. outputList.pop();
  78. }
  79. var frontStr = outputList.pop();
  80. outputList.push(frontStr+endStr);
  81. }
  82. return outputList.join("");
  83. }
  84.  
  85. print(commandParse(message, commandFull));
Success #stdin #stdout 0.35s 41836KB
stdin
Standard input is empty
stdout
Print This Info Add more to the stringDude wh Dont' dropt + that dundnandaanother test