1

I have the code bellow to find the regular expression after %hostname and it replaces all the text correrctly for #hostname.

How do I replicate this for %Location and #Location. I can't seem to get the replication working for more than one variable.

Thanks

var host= "%hostname =(.*)"; // Perl regular expression to find title string            
UltraEdit.activeDocument.top();                                                       

// Turn on regular expressions                                                        
UltraEdit.activeDocument.findReplace.regExp = true;                                   
// Find it                                                                            
UltraEdit.activeDocument.findReplace.find(host);                                      

// Load it into a selection                                                           
var host2= UltraEdit.activeDocument.selection;                                        

// Javascript function 'match' will match the regex within the javascript engine      
// so we can extract the actual title via array                                       
t = host2.match(host);                                                                
savehost = t[1];                                                                      
UltraEdit.activeDocument.top();                                                       
UltraEdit.activeDocument.findReplace.replaceAll=true;                                
UltraEdit.activeDocument.findReplace.matchCase=true;                                 
UltraEdit.activeDocument.findReplace.replace("#hostname", savehost);
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
user3116039
  • 11
  • 1
  • 3

1 Answers1

1

Here is the UltraEdit script for this task.

The array asVariables contains the names of the variables which can be extended by you with additional strings.

if (UltraEdit.document.length > 0)  // Is any file opened?
{
   // Define environment for this script.
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();

   // Define a constant array of variable names which can be extended
   // or modified in script code at any time to support more variables.
   var asVariables = [ "hostname" , "Location" ];

   // Define once all find and replace options to make the
   // script independent on internal defaults of UltraEdit.
   UltraEdit.perlReOn();
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.matchCase=true;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
   {
      UltraEdit.activeDocument.findReplace.searchInColumn=false;
   }
   UltraEdit.activeDocument.findReplace.preserveCase=false;
   UltraEdit.activeDocument.findReplace.replaceAll=true;
   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;

   // Run the code in the loop on all variables defined in the array.
   for (var nVariable = 0; nVariable < asVariables.length; nVariable++)
   {
      // Move caret to top of the active file.
      UltraEdit.activeDocument.top();

      // Perl regular expression to find %variable string and assigned data.
      var sSearch = "%" + asVariables[nVariable] + " *?=.*$";

      UltraEdit.activeDocument.findReplace.regExp=true;

      // If the variable is not found in active file,
      // continue immediately with next variable.
      if(!UltraEdit.activeDocument.findReplace.find(sSearch)) continue;

      // Get just the string after the first equal sign from selected string.
      // The replace method of JavaScript String object never modifies the
      // string value itself. It creates always a new string derived from
      // the current string value with the replace applied.
      var sData = UltraEdit.activeDocument.selection.replace(/^.*?=(.*)$/,"$1");

      // Replace all occurrences of #variable by the found data in entire file.
      UltraEdit.activeDocument.top();
      UltraEdit.activeDocument.findReplace.regExp=false;
      sSearch = "#" + asVariables[nVariable];
      UltraEdit.activeDocument.findReplace.replace(sSearch,sData);
   }
}
Mofi
  • 46,139
  • 17
  • 80
  • 143