The find string must be
"\\{n\\d+\\}" as {...} has a special meaning in Perl regular expressions and therefore { and } must be escaped with a backslash to just find these 2 characters, see the IDM power tip
Getting started with Perl regex in UltraEdit and UEStudio.
However, the script can be made simplier and faster by making the search string a little bit more complicated.
- Code: Select all
if (UltraEdit.document.length > 0)
{
UltraEdit.insertMode();
UltraEdit.columnModeOff();
UltraEdit.activeDocument.hexOff();
UltraEdit.perlReOn();
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=true;
var nNumber = 0;
while (UltraEdit.activeDocument.findReplace.find("(?<=\\{n)\\d+(?=\\})"))
{
nNumber++;
UltraEdit.activeDocument.write(nNumber.toString(10));
}
}
The Perl regular expression string is now:
(?<=\{n
)\d+(?=\}
)This expression means that 1 or more digits should be found and selected. But valid matches are only numbers between {n and }.
(?<=\{n
) is a positive lookbehind expression which consumes zero characters (selects nothing).
(?=\}
) is a positive lookahead expression which consumes also zero characters (selects nothing).
Lookahead and lookbehind are explained in the IDM power tip
Perl Regular Expressions in UltraEdit: Digging Deeper.
As with lookahead and lookbehind only the number is selected and not the entire string {n
X}, it is more easily to replace the selected number by the incrementing number.
The script now renumbers all numbers between
{n and
} in the entire file from top to bottom.
Javascript files have usually the file extension
js. For UltraEdit scripts execution it does not matter which extension the file has. But with extension
js syntax highlighting is applied to the script file making it easier to read the code.