Search code examples
javascriptregexblockly

How to remove highlightBlock strings from generated code


I have a Blockly app that generates the following code as a string.

highlightBlock(':_1ku_aN%|65~:kO;KFA');  
start();  
highlightBlock('3TGkH,fdPJ^sYiBQj{uX');   
moveForward();

This is the function i use to display the code:

function showCode() {
    Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
    var code = Blockly.JavaScript.workspaceToCode(workspace);
    document.getElementById("JSCode").innerText = code;
    console.log(code)
}

How can I remove the highlightBlock('some-id');\n strings? maybe using regex?


Solution

  • You can use index of

    First split the string based on new line character ie using (;), then push all line in an array using loop check array string contain highlight then split the line use rest of other line you your execution;

     code  = code.split(";");
     var newcode = []
     for(var i=0; i<code.length; code++){
      if(code[i].indexOf("highlightBlock") < 0){
    newcode.push(code[i]);
    
       }
    }
    console.log(newcode);
    

    Note: blockly will create id randomly in that semicolon is also part of it please remove that then you wont get any problem in splitting .

    its available in blockly.utils core folder