Inserting tags around place holders entered during script execution if not already present

Inserting tags around place holders entered during script execution if not already present


    Nov 07, 2013#1

    Hi Guys,

    I am very new to Javascript but I am trying to port some old tools that we have (simple regular expressions that get loaded from a txt file).

    In order to do this I need to run certain regular expressions via scripts in UE.

    The first, is quite tricky to explain. I have XML files that contain <ut> </ut> tags. I need a way of adding these tags around place holders. The place holder characters will need to be user specified. For example, the user will input $$ and the regex will look for anything that is surrounded by $$ and add <ut> </ut> around it.

    For example

    $$username$$ will be changed to <ut>$$username$$</ut>.

    The script will need to be smart enough so that it doesn't add tags to place holders that are already inside <ut> tags.

    Any suggestions?

    Grand MasterGrand Master

      Nov 09, 2013#2

      Here is the UltraEdit script for your task:

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
         // Define environment for this script.
         // Move caret to top of the active file.;
         var sPlaceHolder = UltraEdit.getString("Please entere the place holder string:",1);
         // Was any string entered by the script user?
         if (sPlaceHolder.length > 0)
            // Escape every character by with backslash in case of the
            // entered string contains Perl regular expression characters.
            // This replace converts for example $$ to \$\$.
            sPlaceHolder = sPlaceHolder.replace(/(\W)/g,"\\$1");
            // Build the search string for the replace.
            var sSearch = "(?s)(?<!<ut>)("+sPlaceHolder+".+?"+sPlaceHolder+")(?!</ut>)";
            // Define all parameters for the Perl regular expression Replace All.
            if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
            // Run the Perl regular expression Replace All using capturing parentheses
            // and a back reference in the replace string for every found string.
      Explanation of the Perl regular expression string build in string variable sSearch on entering $$ as place holder string:


      (?s) ... this string at beginning of a search string results in a matching of the line ending characters carriage return and line-feed also by . which usually matches only non line ending characters. The closing $$ can be on a different line than the opening $$ with this "flag" at beginning of the search string.

      (?<!<ut>) ... is a negative lookbehind expression. With this expression the Perl regular expression engine looks back after finding opening $$ if there is before <ut>. If this is the case, the $$ are ignored.

      (...) ... informs the Perl regular expression engine to capture (mark/tag) the found string by the expression inside the parentheses for being re-used in search or replace string by a back reference. This is done here with \1 in the replace string. (As the backslash is the escape character also in a JavaScript string, the backslash for the Perl regular expression engine must be additionally escaped with a backslash.)

      \$\$ ... is the place holder string where every character is escaped. This is necessary for $$ as $ means in a Perl regular expression "end of line". With the backslash before which is the escape character in a Perl regular expression search/replace string, the $ is interpreted as character $.

      .+? ... matches any character 1 or more times non greedy. A point matches normally only any non line ending character, but here with (?s) at beginning of the search string it matches really any character. The plus character is a multiplier for the preceding expression (any character) meaning at least 1 times or more. ($$$$ is therefore ignored as there are no characters between opening and closing $$.) The question mark makes the expression non greedy which means stop on first occurrence of next found fixed string which is the closing $$. Without the question mark the expression .+ ould match any character up to last $$ in the file.

      (?!</ut>) ... is a negative lookahead expression. With this expression the Perl regular expression engine looks forward after finding closing $$ if the next characters are not </ut> in which case the search would ignore the found string and restart the search from this position in the file.
      Best regards from an UC/UE/UES for Windows user from Austria