How to delete lines with specific character string?

How to delete lines with specific character string?

1

    Jan 03, 2020#1

    My users used to have a macro with UltraEdit v16.xx that would delete all the lines from an HTML file contained the string [[. We have migrated to the current version of UE for Mac and this macro doesn't work exactly as written anymore and that's OK as I would like to automate their process a little bit more by using UE script.

    I found the delLinesCntgStr.js script that prompts the user for the string they want to find. This script works fine when the user enters \[\[. I tried to hard code the string "\[\[" into the script and comment out the prompting section. When I run my version of the script it fails, telling me I have "entered an invalid regular expression".

    How do I hard-code the [[ search string into this script so the user does not have to respond to a pop-up prompt each time they run it?

    6,602547
    Grand MasterGrand Master
    6,602547

      Jan 03, 2020#2

      The common mistake you have made is described in point 2 of second post of JavaScript tutorial, power tips and more at top of the Scripts forum.

      Here is a commented UltraEdit script optimized for this task.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();
      
         // Define the parameters for a case-sensitive Perl regular
         // expression replace all to delete all lines containing [[.
         UltraEdit.perlReOn();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
      
         // [ has a special meaning in a Perl regular expression (begin of a
         // character class). For that reason the opening square bracket must
         // be escaped with a backslash to be interpreted as literal character
         // by the Perl regular expression engine of UltraEdit. The backslash
         // character is also the escape character in a JavaScript string. For
         // that reason it is necessary to escape both backslashes with one more
         // backslash to really pass the regular expression string with \[\[ from
         // JavaScript core engine to UltraEdit's Perl regular expression engine.
         // The non-marking (non-capturing) group at end selects either a carriage
         // return + line-feed pair (DOS/Windows line termination) or just a
         // line-feed (Unix line termination) or just carriage return (old Mac
         // line termination) or results in selecting till end of file if the
         // last line of a file does not have a line termination, but contains [[.
         UltraEdit.activeDocument.findReplace.replace("^.*\\[\\[.*(?:\\r?\\n|\\r|$)","");
      }
      
      This script is much more efficient (much faster, only one undo step recorded) than user-contributed script delLinesCntgStr.js.
      Best regards from an UC/UE/UES for Windows user from Austria