Tapatalk

Deleting block comments with keeping the number of lines constant

Deleting block comments with keeping the number of lines constant

3
NewbieNewbie
3

    Mar 02, 2010#1

    I'm using UE v15.20.0.1022.

    Being an old-fashioned mainframe techie I know nothing about JS but using the UE documentation and info gleaned from these fora I've managed to create a script to extract certain data from PL/I source code. As part of my data extract I need to preserve the line number on which code is situated. The first part of my script clears down all the comments in the program...

    I initially used...

    UltraEdit.activeDocument.findReplace.regExp=true;
    UltraEdit.activeDocument.findReplace.replaceAll=true;
    UltraEdit.perlReOn();

    UltraEdit.activeDocument.top();

    var searchRegexp = "/\\*(.|[\\r\\n])*?\\*/";
    UltraEdit.activeDocument.findReplace.replace(searchRegexp,"");

    This is fine for comments wholly on a single line, but if a comment spans several lines I need the comment to be replaced with the same number of blank lines so as to preserve subsequent line numbers.

    I did some reading on findreplace.replace and it seems to indicate that the 2nd argument can be a function. Is this true in the implementation of JavaScript in UE? I've tried all sorts but can't seem to get .replace to invoke the function. All I need is for the replace string to contain the same number of CRLF as were in the original comment.

    Can anybody please help.

    Cheers

    262
    MasterMaster
    262

      Mar 02, 2010#2

      Try this :

      Code: Select all

      UltraEdit.activeDocument.findReplace.regExp=true;
      UltraEdit.activeDocument.findReplace.replaceAll=false;
      UltraEdit.perlReOn();
      
      UltraEdit.activeDocument.top();
      
      var searchRegexp = "/\\*(.|[\\r\\n])*?\\*/";
      
      /* Loop through all block of comments with a UE find */
      while (UltraEdit.activeDocument.findReplace.find(searchRegexp)) {
      	
      	/* retrieve currently selected comment block */
      	var selBlock = UltraEdit.activeDocument.selection;
      	
      	/* Remove everything but CRLF thus preserving the number of lines */
      	selBlock = selBlock.replace(/[^\r\n]/g,"");
      
      	/* Now write "empty" block back */
      	UltraEdit.activeDocument.write(selBlock);
      	
      }
      I do not use a function in the replace but a mix of a UE find and a javascript replace. The UE find finds the block of comments using your regExp and in the javascript replace everything but newline (\r\n) is removed and written back into the document as an empty block preserving the number of lines.

      3
      NewbieNewbie
      3

        Mar 02, 2010#3

        Thanks jorrasdk...works brilliantly.