Script for getting footnotes into an HTML document

Script for getting footnotes into an HTML document

20
Basic UserBasic User
20

    May 07, 2012#1

    I have many texts containing strings of type:

    A2B2C2D2E2F2G
    A2B2C2D2E2F2G
    A2B2C2D2E2F2G

    Would anyone know how to write a regex that will increment the integer during each iteration?

    like so:

    A2B2C2D2E2F2G
    A3B3C3D3E3F3G
    A4B4C4D4E4F4G
    ...
    A329B329C329D329E329F329G
    etc.

    I'd be most grateful for help!
    best,
    fvg

    6,681583
    Grand MasterGrand Master
    6,681583

      May 08, 2012#2

      I used the first script posted at How to insert incremental numbers after specific text to create this script making the job for your example. This topic was the first one in the forum search results.

      Code: Select all

      if (UltraEdit.document.length > 0) {
         UltraEdit.insertMode();
         UltraEdit.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.perlReOn();
         var nNumber = 1;
         var sNumber = "1";
         UltraEdit.activeDocument.top();
         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.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=false;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
         while (UltraEdit.activeDocument.findReplace.replace("A\\d+","A"+sNumber)) {
            UltraEdit.activeDocument.findReplace.replace("B\\d+","B"+sNumber);
            UltraEdit.activeDocument.findReplace.replace("C\\d+","C"+sNumber);
            UltraEdit.activeDocument.findReplace.replace("D\\d+","D"+sNumber);
            UltraEdit.activeDocument.findReplace.replace("E\\d+","E"+sNumber);
            UltraEdit.activeDocument.findReplace.replace("F\\d+","F"+sNumber);
            nNumber++;
            sNumber = nNumber.toString();
         }
      }
      It is not very smart, but produces the correct result. However, here is another script very similar using a tagged regular expression Replace in a loop to do the same line by line where it does not matter which characters are before the numbers to replace. I use the UltraEdit regular expression engine in this script because search and replace strings are shorter. The Perl regular expression engine requires backslashes in the expressions which is an escape character in Javascript. Therefore every backslash in the Perl regular expression search and replace strings must be escaped with another backslash. That would make the Perl regular expressions longer and harder to read.

      Code: Select all

      if (UltraEdit.document.length > 0) {
         UltraEdit.insertMode();
         UltraEdit.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.ueReOn();
         UltraEdit.activeDocument.top();
         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.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=false;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
         var nNumber = 0;
         do {
            nNumber++;
            var sNumber = nNumber.toString();
            var sReplace = "^1"+sNumber+"^2"+sNumber+"^3"+sNumber+"^4"+sNumber+"^5"+sNumber+"^6"+sNumber;
         }
         while (UltraEdit.activeDocument.findReplace.replace("%^([~0-9]+^)[0-9]+^([~0-9]+^)[0-9]+^([~0-9]+^)[0-9]+^([~0-9]+^)[0-9]+^([~0-9]+^)[0-9]+^([~0-9]+^)[0-9]+",sReplace));
      }

      20
      Basic UserBasic User
      20

        May 08, 2012#3

        Thank you Mofi. Your'e a real help ...

          May 09, 2012#4

          With Mofi's help I've been making real progress. With the script as I've modified it a bit,

          Code: Select all

          if (UltraEdit.document.length > 0) {
             UltraEdit.insertMode();
             UltraEdit.columnModeOff();
             UltraEdit.activeDocument.hexOff();
             UltraEdit.perlReOn();
             // Footnotes/endnotes to start with this number: (insert here!)
             var nNumber = 1;
             sNumber = nNumber.toString();
             UltraEdit.activeDocument.top();
             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.activeDocument.findReplace.preserveCase=false;
             UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
             UltraEdit.activeDocument.findReplace.replaceAll=true;
             UltraEdit.activeDocument.findReplace.replace("<<","~A1~B1~C1~D1~E1~F1~G");
             UltraEdit.activeDocument.top();
             UltraEdit.activeDocument.findReplace.replaceAll=false;
             while (UltraEdit.activeDocument.findReplace.replace("~A\\d+","~A"+sNumber)) {
                UltraEdit.activeDocument.findReplace.replace("~B\\d+","~B"+sNumber);
                UltraEdit.activeDocument.findReplace.replace("~C\\d+","~C"+sNumber);
                UltraEdit.activeDocument.findReplace.replace("~D\\d+","~D"+sNumber);
                UltraEdit.activeDocument.findReplace.replace("~E\\d+","~E"+sNumber);
                UltraEdit.activeDocument.findReplace.replace("~F\\d+","~F"+sNumber);
                UltraEdit.activeDocument.findReplace.replace("~G\\d+","~G"+sNumber);
                nNumber++;
                sNumber = nNumber.toString();
             }
              UltraEdit.activeDocument.findReplace.replaceAll=true;
              UltraEdit.activeDocument.top();
          //      ALL HTML '"' MUST BE QUOTED OUT!!!!
          //      fn-snippet A:     <a NAME="BACK_
          //      fn-snippet B:     \"></a><a href=\"fvg_hbhm_fn.html#endnote_
          //      fn-snippet C:     \"><sup>[
          //      fn-snippet D:     ]</sup></a>!<a NAME=\"endnote_
          //      fn-snippet E:     \"><p><a HREF=\"fvg_hbhm_text1.html#BACK_
          //      fn-snippet F:     \">[
          //      fn-snippet G:     ]</a>
              UltraEdit.activeDocument.findReplace.replace("~A","<a NAME=\"BACK_");
              UltraEdit.activeDocument.findReplace.replace("~B","\"></a><a href=\"fvg_hbhm_fn.html#endnote_");
              UltraEdit.activeDocument.findReplace.replace("~C","\"><sup>[");
              UltraEdit.activeDocument.findReplace.replace("~D","]</sup></a>!<a NAME=\"endnote_");
              UltraEdit.activeDocument.findReplace.replace("~E","\"><p><a HREF=\"fvg_hbhm_text1.html#BACK_");
              UltraEdit.activeDocument.findReplace.replace("~F","\">[");
              UltraEdit.activeDocument.findReplace.replace("~G","]</a>");
           //     todo:  1) copy file to 'file.tmp'
           //            2) find the fn's in FILE.TMP
           //            3) delete the fn's in current file.
           //            4) insert output file content at end of current file.
             
          }
          I've now got from here:

          Code: Select all

          this is text this is text this is text<<endnote endnote endnote>> 
          this is text this is text this is text this is text this is text 
          this is<<endnote endnote endnote>> text this is text this is text 
          this is text<<endnote endnote endnote>> this is text this is text 
          this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text<<endnote endnote 
          endnote>> this is text this is text this is text this is text this is text this is<<endnote endnote endnote>> text this is text this is text this is text<<endnote 
          endnote endnote>> this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text 
          this is text this is text this is text this is text<<endnote endnote endnote>> this is text this is text this is text this is text<<endnote endnote endnote>> this is text this is text this is text this is text<<endnote endnote endnote>> this is text this is text this is text this is text<<endnote endnote endnote>> this is text 
          to here:

          Code: Select all

          this is text this is text this is text<a NAME="BACK_1"></a><a href="fvg_hbhm_fn.html#endnote_1"><sup>[1]</sup></a>!<a NAME="endnote_1"><p><a HREF="fvg_hbhm_text1.html#BACK_1">[1]</a>endnote endnote endnote>> 
          this is text this is text this is text this is text this is text 
          this is<a NAME="BACK_2"></a><a href="fvg_hbhm_fn.html#endnote_2"><sup>[2]</sup></a>!<a NAME="endnote_2"><p><a HREF="fvg_hbhm_text1.html#BACK_2">[2]</a>endnote endnote endnote>> text this is text this is text 
          this is text<a NAME="BACK_3"></a><a href="fvg_hbhm_fn.html#endnote_3"><sup>[3]</sup></a>!<a NAME="endnote_3"><p><a HREF="fvg_hbhm_text1.html#BACK_3">[3]</a>endnote endnote endnote>> this is text this is text 
          this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text<a NAME="BACK_4"></a><a href="fvg_hbhm_fn.html#endnote_4"><sup>[4]</sup></a>!<a NAME="endnote_4"><p><a HREF="fvg_hbhm_text1.html#BACK_4">[4]</a>endnote endnote 
          endnote>> this is text this is text this is text this is text this is text this is<a NAME="BACK_5"></a><a href="fvg_hbhm_fn.html#endnote_5"><sup>[5]</sup></a>!<a NAME="endnote_5"><p><a HREF="fvg_hbhm_text1.html#BACK_5">[5]</a>endnote endnote endnote>> text this is text this is text this is text<a NAME="BACK_6"></a><a href="fvg_hbhm_fn.html#endnote_6"><sup>[6]</sup></a>!<a NAME="endnote_6"><p><a HREF="fvg_hbhm_text1.html#BACK_6">[6]</a>endnote 
          endnote endnote>> this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text 
          this is text this is text this is text this is text<a NAME="BACK_7"></a><a href="fvg_hbhm_fn.html#endnote_7"><sup>[7]</sup></a>!<a NAME="endnote_7"><p><a HREF="fvg_hbhm_text1.html#BACK_7">[7]</a>endnote endnote endnote>> this is text this is text this is text this is text<a NAME="BACK_8"></a><a href="fvg_hbhm_fn.html#endnote_8"><sup>[8]</sup></a>!<a NAME="endnote_8"><p><a HREF="fvg_hbhm_text1.html#BACK_8">[8]</a>endnote endnote endnote>> this is text this is text this is text this is text<a NAME="BACK_9"></a><a href="fvg_hbhm_fn.html#endnote_9"><sup>[9]</sup></a>!<a NAME="endnote_9"><p><a HREF="fvg_hbhm_text1.html#BACK_9">[9]</a>endnote endnote endnote>> this is text this is text this is text this is text<a NAME="BACK_10"></a><a href="fvg_hbhm_fn.html#endnote_10"><sup>[10]</sup></a>!<a NAME="endnote_10"><p><a HREF="fvg_hbhm_text1.html#BACK_10">[10]</a>endnote endnote endnote>> this is text 
          Which nicely renders to the numbered footnotes that I'm trying to generate.

          What has got me completely stumped is two things: the regex to match the footnote (starting with:
          !<a NAME="endnote_
          and ending with:
          >>

          I thought this would do it:

          Code: Select all

          UltraEdit.activeDocument.findReplace.replace("!<a NAME=\"endnote[ !$A-Z0-9._%+-]+>>","XXXX");
          Or this:

          Code: Select all

          UltraEdit.activeDocument.findReplace.replace("!<a NAME=\"!<a NAME=\"endnote_.*\>\>","XXXX");
          (with or without the quoting "\"s) but no luck, no match. (Some, but not all the matched strings contain <CR><LF>s; the XXXX is just temporary, till I figure out what to do when it finally works.) I've been working on this for days. (I've bought a RegexBuddy, but it hasn't arrived yet.) Beginner's travails...

          Secondly, I can't work out how to move these matched strings - once the regex does work - to the clipboard, to another file, or - preferably - to the end of the current file. (I've been reading through the corresponding posts elsewhere on this list, but it's double Dutch to me...) (Bit of irony there, since I speak Dutch perfectly well - wish I could say the same thing about JS!)
          I'm learning a lot, but any help would be really appreciated...
          fvg

          6,681583
          Grand MasterGrand Master
          6,681583

            May 10, 2012#5

            Look on first endnote string which should be found by your expressions:

            !<a NAME="endnote_1"><p><a HREF="fvg_hbhm_text1.html#BACK_1">[1]</a>endnote endnote endnote>>

            Now take a look on expression [ !$A-Z0-9._%+-]+. What about the characters "#[]/<>= which also exist all in the string to find but are missing in the character set definition in the expression?

            The second expression is better, but does not work well because the expression does not include line terminating characters and is greedy. What you need is:

            UltraEdit.activeDocument.findReplace.replace("(?s)!<a NAME=\"endnote.+?>>","XXXX");

            For an explanation of (?s) see "." in Perl regular expressions doesn't include CRLFs?

            The expressions .+ is by default a greedy expression. That means it matches as much as possible. So it does not stop on first occurrence of >> after string !<a NAME="endnote, it stops left to last occurrence of >> which is not what you want as this would result in selecting multiple endnotes with everything between. With .+? the expression becomes non greedy and now matching any character stops left to first occurrence of >>.


            A string found with command UltraEdit.activeDocument.findReplace.find() is automatically also selected. You have 3 choices to run a string collecting loop.

            Version 1: Using a user clipboard and command Copy & Append.

            Code: Select all

            // Use user clipboard 9 instead of Windows clipboard.
            // This version should be used if Unicode characters are matched too.
            UltraEdit.selectClipboard(9);
            UltraEdit.clearClipboard();
            UltraEdit.activeDocument.top();
            while(UltraEdit.activeDocument.findReplace.find("...whatever...")) {
               UltraEdit.activeDocument.copyAppend();
               UltraEdit.clipboardContent += "\r\n";
            }
            UltraEdit.activeDocument.bottom();
            UltraEdit.activeDocument.paste();
            UltraEdit.clearClipboard();
            UltraEdit.selectClipboard(0);
            Version 2: Using a user clipboard and directly append a found string.

            The second version is the same as above with the difference that instead of UltraEdit.activeDocument.copyAppend(); the following code is used:

            Code: Select all

               UltraEdit.clipboardContent += UltraEdit.activeDocument.selection;
            This is not working for Unicode strings. Therefore there is just a disadvantage and no advantage in comparison to first version. I have posted this version just for completness.

            Version 3: Collecting found strings in a dynamic Javascript string variable.

            Code: Select all

            var sFoundStrings = "";
            UltraEdit.activeDocument.top();
            while(UltraEdit.activeDocument.findReplace.find("...whatever...")) {
               sFoundStrings += UltraEdit.activeDocument.selection;
               sFoundStrings += "\r\n";
            }
            UltraEdit.activeDocument.write(sFoundStrings);
            This version does not work for Unicode strings. And as I found out once without knowing the reason this method is much slower than using the first version with using the clipboard for large files.

            Please note that if the Find is a Perl regular expression find you might need as last command within while loop UltraEdit.activeDocument.cancelSelect(); to avoid an endless loop finding last occurrence again and again.
            With UE or Unix regular expressions finds or with a non regular expression find this command is not needed.

            BTW: Your HTML code is invalid. You have block element <p> embedded in inline element <a>.

            20
            Basic UserBasic User
            20

              May 10, 2012#6

              That worked like a charm, with accolades to Mofi!
              This script now correctly collects all footnotes from a text and moves them to the bottom of the file:
              From this:

              Code: Select all

              this is text this is text this is text<<endnote endnote endnote>> 
              this is text this is text this is text this is text this is text 
              this is<<endnote endnote endnote>> text this is text this is text 
              this is text<<endnote endnote endnote>> this is text this is text 
              this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text<<endnote endnote <CR>
              endnote>> this is text this is text this is text this is text this is text this is<<endnote endnote endnote>> text this is text this is text this is text<<endnote <CR>
              endnote endnote>> this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text 
              this is text this is text this is text this is text<<endnote endnote endnote>> this is text this is text this is text this is text<<endnote endnote endnote>> this is text this is text this is text this is text<<endnote endnote endnote>> this is text this is text this is text this is text<<endnote endnote endnote>> this is text
              to this:

              Code: Select all

              this is text this is text this is text<a NAME="BACK_1"></a><a href="fvg_hbhm_fn.html#endnote_1"><sup>[1]</sup></a> 
              this is text this is text this is text this is text this is text 
              this is<a NAME="BACK_2"></a><a href="fvg_hbhm_fn.html#endnote_2"><sup>[2]</sup></a> text this is text this is text 
              this is text<a NAME="BACK_3"></a><a href="fvg_hbhm_fn.html#endnote_3"><sup>[3]</sup></a> this is text this is text 
              this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text<a NAME="BACK_4"></a><a href="fvg_hbhm_fn.html#endnote_4"><sup>[4]</sup></a> this is text this is text this is text this is text this is text this is<a NAME="BACK_5"></a><a href="fvg_hbhm_fn.html#endnote_5"><sup>[5]</sup></a> text this is text this is text this is text<a NAME="BACK_6"></a><a href="fvg_hbhm_fn.html#endnote_6"><sup>[6]</sup></a> this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text 
              this is text this is text this is text this is text<a NAME="BACK_7"></a><a href="fvg_hbhm_fn.html#endnote_7"><sup>[7]</sup></a> this is text this is text this is text this is text<a NAME="BACK_8"></a><a href="fvg_hbhm_fn.html#endnote_8"><sup>[8]</sup></a> this is text this is text this is text this is text<a NAME="BACK_9"></a><a href="fvg_hbhm_fn.html#endnote_9"><sup>[9]</sup></a> this is text this is text this is text this is text<a NAME="BACK_10"></a><a href="fvg_hbhm_fn.html#endnote_10"><sup>[10]</sup></a> this is text <a NAME="endnote_1"><p><a HREF="fvg_hbhm_text1.html#BACK_1">[1]</a>endnote endnote endnote
              <a NAME="endnote_2"><p><a HREF="fvg_hbhm_text1.html#BACK_2">[2]</a>endnote endnote endnote
              <a NAME="endnote_3"><p><a HREF="fvg_hbhm_text1.html#BACK_3">[3]</a>endnote endnote endnote
              <a NAME="endnote_4"><p><a HREF="fvg_hbhm_text1.html#BACK_4">[4]</a>endnote endnote <CR>
              endnote
              <a NAME="endnote_5"><p><a HREF="fvg_hbhm_text1.html#BACK_5">[5]</a>endnote endnote endnote
              <a NAME="endnote_6"><p><a HREF="fvg_hbhm_text1.html#BACK_6">[6]</a>endnote <CR>
              endnote endnote
              <a NAME="endnote_7"><p><a HREF="fvg_hbhm_text1.html#BACK_7">[7]</a>endnote endnote endnote
              <a NAME="endnote_8"><p><a HREF="fvg_hbhm_text1.html#BACK_8">[8]</a>endnote endnote endnote
              <a NAME="endnote_9"><p><a HREF="fvg_hbhm_text1.html#BACK_9">[9]</a>endnote endnote endnote
              <a NAME="endnote_10"><p><a HREF="fvg_hbhm_text1.html#BACK_10">[10]</a>endnote endnote endnote
              (Which correctly renders in my browser. I'll tweak that HTML later, Mofi, soon as I've understood what's wrong with it..).
              But there will be other people out there who need to use footnotes on a web document, so here is the script ... see my next post for improved version.

              6,681583
              Grand MasterGrand Master
              6,681583

                May 10, 2012#7

                Well, you have used once fvg_hbhm_fn.html and once fvg_hbhm_text1.html. I don't think that this is a good idea if text and footnotes are in same HTML file.

                Better would be the output:

                Code: Select all

                this is text this is text this is text<a href="#endnote_1" name="BACK_1"><sup>[1]</sup></a> 
                this is text this is text this is text this is text this is text 
                this is<a href="#endnote_2" name="BACK_2"><sup>[2]</sup></a> text this is text this is text 
                this is text<a href="#endnote_3" name="BACK_3"><sup>[3]</sup></a> this is text this is text 
                this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text<a href="#endnote_4" name="BACK_4"><sup>[4]</sup></a> this is text this is text this is text this is text this is text this is<a href="#endnote_5" name="BACK_5"><sup>[5]</sup></a> text this is text this is text this is text<a href="#endnote_6" name="BACK_6"><sup>[6]</sup></a> this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text 
                this is text this is text this is text this is text<a href="#endnote_7" name="BACK_7"><sup>[7]</sup></a> this is text this is text this is text this is text<a href="#endnote_8" name="BACK_8"><sup>[8]</sup></a> this is text this is text this is text this is text<a href="#endnote_9" name="BACK_9"><sup>[9]</sup></a> this is text this is text this is text this is text<a href="#endnote_10" name="BACK_10"><sup>[10]</sup></a> this is text
                <p><a href="#BACK_1" name="endnote_1">[1]</a>endnote endnote endnote</p>
                <p><a href="#BACK_2" name="endnote_2">[2]</a>endnote endnote endnote</p>
                <p><a href="#BACK_3" name="endnote_3">[3]</a>endnote endnote endnote</p>
                <p><a href="#BACK_4" name="endnote_4">[4]</a>endnote endnote <CR>
                endnote</p>
                <p><a href="#BACK_5" name="endnote_5">[5]</a>endnote endnote endnote</p>
                <p><a href="#BACK_6" name="endnote_6">[6]</a>endnote <CR>
                endnote endnote</p>
                <p><a href="#BACK_7" name="endnote_7">[7]</a>endnote endnote endnote</p>
                <p><a href="#BACK_8" name="endnote_8">[8]</a>endnote endnote endnote</p>
                <p><a href="#BACK_9" name="endnote_9">[9]</a>endnote endnote endnote</p>
                <p><a href="#BACK_10" name="endnote_10">[10]</a>endnote endnote endnote</p>
                NAME and HREF are both attributes of inline element A. Both attributes can be used in same element instance. There is no need to have two instances of A element, one with NAME attribute and another with HREF attribute. According to HTML standard A elements with no data (= nothing between <A> and </A>) should be avoided and can be always avoided. If an anchor is needed without being at same time also a hyperlink, the universal attribute ID can be used in any element instead of using <A NAME="..."></A>.

                20
                Basic UserBasic User
                20

                  May 12, 2012#8

                  Right, got it.. Superfluous </a><a>'s deleted, link/anchor sequence reversed. (My guess is, that's not strictly necessary?)
                  (My original code was intended for use with long web documents, consisting of different chapters, with the endnotes all accumulating into a single, separate, ENDNOTEFILE.html. That's how the publishers usually want it.)

                  Code: Select all

                  if (UltraEdit.document.length > 0) {
                     UltraEdit.insertMode();
                     UltraEdit.columnModeOff();
                     UltraEdit.activeDocument.hexOff();
                       //UltraEdit.ueReOn();     // UltraEdit
                       //UltraEdit.unixReOn();   // Unix 
                     UltraEdit.perlReOn();       // Perl
                       // Change variable nNumber if first Footnote is to be > 1:
                     var nNumber = 1;
                     sNumber = nNumber.toString();
                     UltraEdit.activeDocument.top();
                     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.activeDocument.findReplace.preserveCase=false;
                     UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
                     UltraEdit.activeDocument.findReplace.replaceAll=true;
                     UltraEdit.activeDocument.findReplace.replace("<<","~A1~B1~C1~D1~E1~F1~G");
                     UltraEdit.activeDocument.top();
                     UltraEdit.activeDocument.findReplace.replaceAll=false;
                     while (UltraEdit.activeDocument.findReplace.replace("~A\\d+","~A"+sNumber)) {
                        UltraEdit.activeDocument.findReplace.replace("~B\\d+","~B"+sNumber);
                        UltraEdit.activeDocument.findReplace.replace("~C\\d+","~C"+sNumber);
                        UltraEdit.activeDocument.findReplace.replace("~D\\d+","~D"+sNumber);
                        UltraEdit.activeDocument.findReplace.replace("~E\\d+","~E"+sNumber);
                        UltraEdit.activeDocument.findReplace.replace("~F\\d+","~F"+sNumber);
                        UltraEdit.activeDocument.findReplace.replace("~G\\d+","~G"+sNumber);
                        nNumber++;
                        sNumber = nNumber.toString();
                     }
                      UltraEdit.activeDocument.findReplace.replaceAll=true;
                      UltraEdit.activeDocument.top();
                  //  full string:  <a href=\"endnote_x\" name=\"BACK_x\"><sup>[x]</sup></a>
                  //  full string:  <p><a NAME=\"endnote_x\" HREF=\"BACK_x\">[x]</a>
                  //      ALL HTML '"' MUST BE QUOTED OUT!!!!
                  //          snippetA:   <a href=\"endnote_
                  //          snippetB:   \" name=\"BACK_
                  //          snippetC:   \"><sup>[
                  //          snippetD:   ]</sup></a><p><a NAME=\"endnote_
                      /       snippetE:   \" HREF=\"BACK_
                  //          snippetF:   \">[
                  //          snippetG:   ]</a>  
                      UltraEdit.activeDocument.findReplace.replace("~A","<a href=\"endnote_");
                      UltraEdit.activeDocument.findReplace.replace("~B","\" name=\"BACK_");
                      UltraEdit.activeDocument.findReplace.replace("~C","\"><sup>[");
                      UltraEdit.activeDocument.findReplace.replace("~D","]</sup></a><p><a NAME=\"endnote_");
                      UltraEdit.activeDocument.findReplace.replace("~E","\" HREF=\"BACK_");
                      UltraEdit.activeDocument.findReplace.replace("~F","\">[");
                      UltraEdit.activeDocument.findReplace.replace("~G","]</a>");
                      UltraEdit.selectClipboard(9);
                      UltraEdit.clearClipboard();
                      UltraEdit.activeDocument.top();
                      while(UltraEdit.activeDocument.findReplace.find("(?s)<p><a NAME=\"endnote.+?>>")) {
                        UltraEdit.activeDocument.copyAppend();
                        UltraEdit.clipboardContent += "\r\n";
                      }                                   // <---END OF WHILE! (DON'T DELETE)
                      UltraEdit.activeDocument.top();
                      UltraEdit.activeDocument.findReplace.replace("(?s)<p><a NAME=\"endnote.+?>>","");
                      UltraEdit.activeDocument.bottom();
                      UltraEdit.activeDocument.paste();
                      UltraEdit.clearClipboard();
                      UltraEdit.selectClipboard(0);
                      UltraEdit.activeDocument.top();
                      UltraEdit.activeDocument.findReplace.replace(">>","</p>");
                    
                  }    // <---END OF "IF" IN LINE 1! (DON'T DELETE)
                  
                  // EOF  (With thanks to Mofi!)
                  

                    Jan 18, 2014#9

                    It's two years on, but I'm still having problems with this bit of code. I want to iterate through all instances of "<<" in this file, replace it with a bit of code, and then increment a number.

                    What I've now managed to get to work, to date, is this, and it seems not to mind which of the regex flavours is switched on:

                    Code: Select all

                    if (UltraEdit.document.length > 0) {
                       var nNumber = 0;
                       var sReplace1 = "";
                       var sReplace2 = "";
                       UltraEdit.insertMode();
                       UltraEdit.columnModeOff();
                       UltraEdit.activeDocument.hexOff();
                       UltraEdit.perlReOn();   // Regex Perl
                       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;
                       UltraEdit.activeDocument.findReplace.searchInColumn=false;
                       UltraEdit.activeDocument.findReplace.preserveCase=false;
                       UltraEdit.activeDocument.findReplace.replaceAll=false;
                       UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
                       UltraEdit.activeDocument.top();
                       do {
                          nNumber++;
                          sReplace1 = nNumber.toString();
                          sReplace2 = "<a href=\"#endnote_"+sReplace1+"\" id=\"BACK_"+sReplace1+"\"><sup>["+sReplace1+"]</sup></a><p><a id=\"endnote_"+sReplace1+"\" HREF=\"#BACK_"+sReplace1+"\">["+sReplace1+"]</a> ";
                       } while (UltraEdit.activeDocument.findReplace.replace("<<",sReplace2));
                    }
                    
                    What the script should do is replace all the '<<':

                    Code: Select all

                    this is text this is text this is text<<endnote endnote endnote>> 
                    this is text this is text this is text this is text this is text 
                    this is<<endnote endnote endnote>> text this is text this is text 
                    this is text<<endnote endnote endnote>> this is text this is text 
                    this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text<<endnote endnote 
                    endnote>> this is text this is text this is text this is text this is text this is<<endnote endnote endnote>> text this is text this is text this is text<<endnote 
                    endnote endnote>> this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text 
                    this is text this is text this is text this is text<<endnote endnote endnote>> this is text this is text this is text this is text<<endnote endnote endnote>> this is text this is text this is text this is text<<endnote endnote endnote>> this is text this is text this is text this is text<<endnote endnote endnote>> this is text 
                    So that it ends up with this:

                    Code: Select all

                    this is text this is text this is text(HTML_snippet1 snippet1 snippet1 snippet1) endnote endnote endnote>> 
                    this is text this is text this is text this is text this is text 
                    this istext(HTML_snippet2 snippet2 snippet2 snippet2) endnote endnote endnote>> text this is text this is text 
                    this is text(HTML_snippet3 snippet3 snippet3 snippet3)endnote endnote endnote>> this is text this is text 
                    this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text(HTML_snippet4 snippet4 snippet4 snippet4)endnote endnote 
                    endnote>> this is text this is text this is text this is text this is text this is(HTML_snippet5 snippet5 snippet5 snippet5)endnote endnote endnote>> text this is text this is text this is text(HTML_snippet6 snippet6 snippet6 snippet6)endnote 
                    endnote endnote>> this is text this is text this is text this is text this is text this is text this is text this is text this is text this is text 
                    this is text this is text this is text this is text(HTML_snippet7 snippet7 snippet7 snippet7)endnote endnote endnote>> this is text this is text this is text this is text(HTML_snippet8 snippet8 snippet8 snippet8)endnote endnote endnote>> this is text this is text this is text this is text(HTML_snippet9 snippet9 snippet9 snippet9)endnote endnote endnote>> this is text this is text this is text this is text(HTML_snippet10 snippet10 snippet10 snippet10)endnote endnote endnote>> this is text 
                    The original code above, which Mofi kindly provided, sometimes works, but sometimes it doesn't, and I'm having problems debugging it. So I'm trying something else, to see if this works instead. I'd be really grateful for a bit of help. Getting footnotes into a HTML file seems to be something a lot of people are interested in.
                    best,
                    fvgfvg

                    6,681583
                    Grand MasterGrand Master
                    6,681583

                      Jan 19, 2014#10

                      Well, the search and the replace string are both non regular expression strings. Therefore it is not necessary to enable the regular expression option for the replaces executed in a loop.

                      And in JavaScript it is possible to define strings either with single or with double quotes.

                      If the string starts and ends with double quotes, double quotes within the string must be escaped with a backslash and single quotes within the string must not be escaped.

                      If the string starts and ends with single quotes, single quotes within the string must be escaped with a backslash and double quotes within the string must not be escaped.

                      Your replace string contains several double quotes and therefore it is better to define the strings with single quotes.

                      Code: Select all

                      if (UltraEdit.document.length > 0) {
                         UltraEdit.insertMode();
                         UltraEdit.columnModeOff();
                         UltraEdit.activeDocument.hexOff();
                         UltraEdit.perlReOn();   // Regex Perl
                         UltraEdit.activeDocument.findReplace.mode=0;
                         UltraEdit.activeDocument.findReplace.matchCase=false;
                         UltraEdit.activeDocument.findReplace.matchWord=false;
                         UltraEdit.activeDocument.findReplace.regExp=false;
                         UltraEdit.activeDocument.findReplace.searchDown=true;
                         UltraEdit.activeDocument.findReplace.searchInColumn=false;
                         UltraEdit.activeDocument.findReplace.preserveCase=false;
                         UltraEdit.activeDocument.findReplace.replaceAll=false;
                         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
                         UltraEdit.activeDocument.top();
                         var nNumber = 0;
                         var sNumber = "";
                         var sReplace = "";
                         do {
                            nNumber++;
                            sNumber = nNumber.toString();
                            sReplace = '<a href="#endnote_'+sNumber+'" id="BACK_'+sNumber+'"><sup>['+sNumber+']</sup></a><p><a id="endnote_'+sNumber+'" HREF="#BACK_'+sNumber+'">['+sNumber+']</a> ';
                         } while (UltraEdit.activeDocument.findReplace.replace("<<",sReplace));
                      }
                      Best regards from an UC/UE/UES for Windows user from Austria

                      20
                      Basic UserBasic User
                      20

                        Jan 19, 2014#11

                        Wow.. didn't know that... thank you.