UltraEdit scripting using JavaScript and wildcards

1.9k Views Asked by At

I have a program using a combination of JavaScript and UltraEdit scripting. The program has an array of strings to search for in a file/tab. If found, it moves the corresponding lines to a new file/tab. When using exact match it works great.

However, my source values are not exact matches. The values in the file are ######-## where the values after the dash vary. I have the value up to the dash. I attempted to build the wildcard into the array values, and I've attempted to concatenate it to the .find function with no success. Any thoughts would be greatly appreciated.

Here is the code I'm executing as a script within UltraEdit. I've truncated the array from the 50 values it contained for the purpose of demonstration.

// Start at the beginning of the file
UltraEdit.activeDocument.top();

// Search string variable used for copying of lines
//DD011881 - Building an array of values
var delString = new Array()
delString[0] = "'99999999'";
delString[1] = "'169-*'";
delString[2] = "'5482-*'";
delString[3] = "'5998-*'";
delString[4] = "'36226-*'";
delString[5] = "'215021-*'";


// Array loop value
var x = 0;
var arrayLen = delString.length

// Start with nothing on the clipboard
UltraEdit.clearClipboard();

for (x=0; x<arrayLen; x++)
{

    // Establish our search string for the loop condition
    var bFound = false;

    while (UltraEdit.activeDocument.findReplace.find(delString[x])){

        UltraEdit.activeDocument.selectLine();
        UltraEdit.activeDocument.copyAppend("^c" + "\n");
        bFound = true;
    }

    UltraEdit.activeDocument.top();
    if (bFound) {
        UltraEdit.document[6].paste();
        UltraEdit.activeDocument.top();
        UltraEdit.clearClipboard();
    }
} // For Loop
1

There are 1 best solutions below

0
On

In your UltraEdit script you want to run an UltraEdit regular expression find in the while loop, but you have never set the regular expression engine nor any find parameter. So the script is executing the find with the internal defaults for finds (case-insensitive, non-regular expression search downwards, not matching whole words with the Perl regular expression engine selected).

Insert in your UltraEdit script below the command UltraEdit.clearClipboard(); the following lines:

UltraEdit.ueReOn();
UltraEdit.activeDocument.findReplace.mode = 0;
UltraEdit.activeDocument.findReplace.matchCase = true;
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;
}

Now the UltraEdit regular expression is selected for the script and the find parameters are set for running a case-sensitive (faster) regular expression search.

And please remove "^c" + "\n" from command UltraEdit.activeDocument.copyAppend() as this command does not take any parameter. With the command above, the entire line including the line termination is already selected and this selection is appended to the clipboard, not the string you put into the parentheses of command copyAppend().