gpt4 book ai didi

javascript - 在UltraEdit中搜索关键字

转载 作者:行者123 更新时间:2023-12-03 10:25:47 26 4
gpt4 key购买 nike

我以前从未使用过UltraEdit,因此遇到了困难。我看了看文档,没有发现任何帮助。

截至目前,我只是试图在用户已打开的当前文件中查找一个单词,并将从文件开头到单词的所有内容都删除。

我尝试了各种功能

UltraEdit.activeDocument


我认为这是我需要使用的,但是没有一个起作用。我已经尝试过类似的事情

search
goto


但他们都失败了。我无法在用户打开的文件中搜索任何内容。

对此的任何帮助,或者可能是带有javascript的UltraEdit的指南用户指南。

最佳答案

这可以在UltraEdit中使用正则表达式Replace All从文件顶部执行,或者使用高级替换选项Replace all在文件顶部执行。

单击“替换”对话框中的“帮助”按钮,然后单击指向说明UltraEdit / Unix和Perl正则表达式语法的页面的链接。通过单击用于打开正则表达式构建器列表的按钮(带放大镜或三角形的按钮,具体取决于UE的版本),在“替换”对话框中也可以直接获得帮助。

UltraEdit正则表达式全部替换

将UltraEdit正则表达式引擎与tagged regular expression一起使用时,搜索字符串为%*^(word^),替换字符串仅为^1


%表示一行的开始。
*表示除回车和换行0次或多次外的任何字符。
^( ... ^)word标记为^1引用的替换,将其保留为word,而在运行不区分大小写的替换时,请勿更改word中任何字母的大小写。


搜索字符串是非贪婪的,这意味着*在一行中第一次出现word时将停止匹配字符。

匹配直到一行中最后出现word的所有内容的贪婪搜索表达式将为%?+^(word^)?+表示除换行符回车和换行1次或多次以外的任何字符。

注意:非贪婪的表达式也用行首的word进行替换,尽管实际上没有任何替换/删除。但是,行更改指示器仍会将该行标记为已修改。

Unix正则表达式全部替换

使用Unix正则表达式引擎,搜索字符串需要为^.*(word),替换字符串为\1


^表示一行的开始。
.*表示除回车和换行0或更多非贪婪字符以外的任何字符。
( ... )word标记为\1所引用的替换,因为word应该保留。


贪婪搜索表达式为^.+(test).+表示1次或多次。

注意:非贪婪的表达式也用行首的word进行替换,尽管实际上没有任何替换/删除。

Perl正则表达式全部替换

使用功能最强大的Perl正则表达式引擎,可以完成此任务。

可以将Perl regular expression using backreferences与UltraEdit / Unix正则表达式引擎一起使用,对非贪婪的搜索字符串^.*?(word)^.+?(word)或对贪婪的搜索使用^.*(word)^.+(word)并使用\1作为替换字符串。

从这里可以看出,Perl正则表达式引擎具有一种特殊的语法,它确实定义了一个乘数,例如*(0次或多次)或+(1次或多次)贪婪或非贪婪。乘数后面的?会使表达式变得非贪婪。

使用搜索字符串^.+?(word)可以避免在以word开头的行上进行替换时保持不变。

但是Perl正则表达式引擎甚至更支持不匹配的lookahead

为此任务使用超前表达式的Perl正则表达式搜索字符串为^.+?(?=word),替换字符串现在为空字符串。

为什么要使用空的替换字符串?

现在替换字符串必须为空,因为word现在不再与搜索匹配,只是从行首到word首次出现的字符串至少包含1个字符。

但这不是Perl正则表达式引擎支持的全部。如果word是较长单词的子字符串,替换将删除从行首到第一次出现的所有字符,而word是整个单词而不是较长单词的一部分,该怎么办?

好吧,Perl正则表达式引擎支持\<表示单词的开头,\>表示单词的结尾,而\b表示任何单词的边界(开头或结尾)。

例如,^.+?(?=\<word\>)^.+?(?=\bword\b)将忽略仅包含words但不包含word的行。

UltraEdit脚本要求用户输入单词

上面说明的所有可能性也可以在UltraEdit宏和脚本中使用。

脚本解决方案要求脚本用户输入一个单词,然后替换从行开头到活动文件中首次出现输入单词的所有内容的必要代码为:

if (UltraEdit.document.length > 0)  // Is any file opened?
{
// Ask user of the script for the word to search for.
var sWord = UltraEdit.getString("Please enter the word:",1);

// Has the script user entered anything at all?
if (sWord.length)
{
// Has the user really entered a word, i.e. the string does not contain
// any non word character (not being a letter, digit or underscore)?
if (sWord.search(/\W/) < 0)
{
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();

// Move caret to top of the active file.
UltraEdit.activeDocument.top();

// Define all the Perl regular expression Replace All options.
UltraEdit.perlReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=false;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=true;
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;

// In JavaScript strings the backslash character is the escape
// character. Therefore it is necessary to escape each backslash
// with one more backslash to pass to the Perl regular expression
// engine the search string containing the two backslashes.

// Execute the Replace All from top of file.
UltraEdit.activeDocument.findReplace.replace("^.+?(?=\\<"+sWord+"\\>)","");
}
}
}

关于javascript - 在UltraEdit中搜索关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29406652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com