Let's say there is a file containing the following line:
Code: Select all
Some text before <$!!TITLE_Example for a title> and some text after the title.
And we run on this file a script with following code:
Code: Select all
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
UltraEdit.perlReOn();
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;
UltraEdit.activeDocument.findReplace.searchInColumn=false;
UltraEdit.outputWindow.showStatus=false;
UltraEdit.outputWindow.clear();
UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.write("First version of getting the title:\n===================================");
UltraEdit.activeDocument.top();
if(UltraEdit.activeDocument.findReplace.find("<\\$!!TITLE_.*?>"))
{
var sFoundTitle = UltraEdit.activeDocument.selection;
sTitle1 = sFoundTitle.replace(/<\$!!TITLE_(.*?)>/,"$1");
UltraEdit.outputWindow.write("sFoundTitle = "+sFoundTitle);
UltraEdit.outputWindow.write("sTitle1 = "+sTitle1);
}
UltraEdit.outputWindow.write("\nSecond version of getting the title:\n====================================");
UltraEdit.activeDocument.top();
if(UltraEdit.activeDocument.findReplace.find("<\\$!!TITLE_.*?>"))
{
var sTitle2 = UltraEdit.activeDocument.selection;
UltraEdit.outputWindow.write("sTitle2 (before replace) = "+sTitle2);
sTitle2 = sTitle2.replace(/<\$!!TITLE_(.*?)>/,"$1");
UltraEdit.outputWindow.write("sTitle2 (after replace) = "+sTitle2);
}
UltraEdit.outputWindow.write("\nThird version of getting the title:\n===================================");
UltraEdit.activeDocument.top();
if(UltraEdit.activeDocument.findReplace.find("<\\$!!TITLE_.*?>"))
{
var sTitle3 = UltraEdit.activeDocument.selection.replace(/<\$!!TITLE_(.*?)>/,"$1");
UltraEdit.outputWindow.write("sTitle3 = "+sTitle3);
}
UltraEdit.outputWindow.write("\nFourth version of getting the title:\n====================================");
UltraEdit.activeDocument.top();
if(UltraEdit.activeDocument.findReplace.find("(?<=<\\$!!TITLE_)[^>]+"))
{
var sTitle4 = UltraEdit.activeDocument.selection;
UltraEdit.outputWindow.write("sTitle4 = "+sTitle4);
}
}
The output window would contain now the information:
Code: Select all
First version of getting the title:
===================================
sFoundTitle = <$!!TITLE_Example for a title>
sTitle1 = Example for a title
Second version of getting the title:
====================================
sTitle2 (before replace) = <$!!TITLE_Example for a title>
sTitle2 (after replace) = Example for a title
Third version of getting the title:
===================================
sTitle3 = Example for a title
Fourth version of getting the title:
====================================
sTitle4 = Example for a title
You see here the 4 possibilities to get the string you want into a variable.
About first version:
The first version finds the entire title string plus the surrounding tags you do not want. The round brackets are useless here as you do not back reference the part of the found string matched by .*? directly in the search string nor is the command a replace command. It is not possible to get a part of the found string hold internally in the Perl regular find/replace function for the find/replace only outside the function.
The found string is selected by UltraEdit in the active document and UltraEdit creates a copy of the selected text into a JavaScript string object. This text is next copied once more in memory to one more dynamically created string object with name sFoundTitle. Next a replace is done on the value of sFoundTitle. But the replace method of a JavaScript String object never modifies the string value of the string object on which the replace method is applied. The replace command always results in creating a new string in memory which is next assigned as a value to a new string object with name sTitle1.
About second version:
This is nearly the same as first version. The difference is that after creating in memory the new string with the replaces applied, the original and up to this point still unmodified string object sTitle2 is destroyed (removed from memory) and a new string object is created with name sTitle2 (same as the other string object before) which is associated with the newly created string value in memory.
About third version:
As UltraEdit.activeDocument.selection is a JavaScript string object containing a copy of the selected text in active document, it is possible to apply the replace method directly on this string object. As the replace method does not replace the value of string object UltraEdit.activeDocument.selection, but creates a new string in memory with the modifications according to regular expression search string and the replace string applied, the new string object sTitle3 created next contains then as value the string of interest.
About fourth version:
The Perl regular expression search string is modified to use a positive lookbehind resulting in getting found and therefore selected only the part of the entire title tag which should be really found and processed further.