javascript - UltraEdit 查找和替换正则表达式

标签 javascript regex ultraedit

我有下面的代码来查找 %hostname 之后的正则表达式,它正确地替换了 #hostname 的所有文本。

我如何为 %Location#Location 复制这一点。我似乎无法让复制工作于多个变量。

谢谢

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);

最佳答案

这是用于此任务的 UltraEdit 脚本。

数组 asVariables 包含变量的名称,您可以使用其他字符串对其进行扩展。

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);
   }
}

关于javascript - UltraEdit 查找和替换正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23088869/

相关文章:

javascript - 点击跟踪器的颜色变化

javascript - iFrame 是好是坏?

c# - 尝试在 C# 上复制 JavaScript 哈希值

javascript - JavaScript 中的正则表达式在 uglifyjs 或 minify 运行后替换函数中断脚本

replace - 在 Notepad++ 或 UltraEdit 中将每行的行首移动到行尾

javascript - 请帮助我理解这段来自 "Ajax in Action"的 JavaScript 代码

java - JFileChooser和.txt文件解析问题

regex - 如果 URI 包含单词,则在 Apache .htaccess 中设置 header

html - UltraEdit 中的 XML 缩进