Why does a call of string function match with a regular expression with a look-behind result in an assertion?

Why does a call of string function match with a regular expression with a look-behind result in an assertion?

2
NewbieNewbie
2

    Mar 25, 2022#1

    Hello,

    I wanted to match a string within an expression with a preceding space, for example:

    peach in apple peach

    Selecting the relevant part beforehand with UltraEdit.activeDocument.findReplace.find() or manually, my idea was the following:

    Code: Select all

    var relevant_fruits = UltraEdit.activeDocument.selection.match(/(?<= )[\w]*/g);
    Running the script, with apple peach strawberry watermelon selected, should return the array relevant_fruits with ["peach", "strawberry", "watermelon"].

    Unfortunately I get an error message regarding the line stated above.

    Does anyone know why?

    Thanks in advance! :)

    6,603548
    Grand MasterGrand Master
    6,603548

      Mar 25, 2022#2

      You have luck that I can answer this question. I have asked just a week ago UltraEdit support about which version of the JavaScript engine is embedded in UltraEdit released finally as version 2022.0.0.70 two days ago and have got following reply:
      Our JavaScript engine should support 1.8.1 of JavaScript and 3.1 of ECMAScript.

      We are planning now to update our scripting engine in a future release.
      Support for look-behind regular expression by the RegExp object was added with ECMAScript 2018. The referenced Mozilla page for the RegExp object contains a table which lists which version of the most often used web browsers support a look-behind expression which is fine to know for web page developers, but is of no real help here. Mozilla's JavaScript core engine is not versioned anymore with an own version number. For more details see the Stack Overflow question: What are the version numbers of JavaScript and what ECMAScript version do they correspond to?

      So the reason for the error message on script execution is that the JavaScript core engine embedded in UltraEdit does not support regular expressions with look-behind and throws therefore an exception.

      The Perl compatible regular expression engine embedded in UltraEdit supports look-behind expressions since years more or less perfect depending on the amount of characters to search for and the version of UltraEdit as this engine is continuously updated, last for just released UltraEdit for Windows v2022.0.0.70. The Perl compatible regular expression engine embedded in UltraEdit is from the Boost C++ libraries.

      There can be used one of the two following code examples to get an array of strings with the fruit names not including the first one apple.

      The inefficient solution using function match with the advantage that it does not matter which and how many non-word characters are between the words (fruit names).

      Code: Select all

      var relevant_fruits = UltraEdit.activeDocument.selection.match(/\b\w+\b/g);
      if (relevant_fruits !== null)
      {                                // Remove the first element from the array.
         relevant_fruits.splice(0,1);  // That can result in an empty array if the
      }                                // selected text contains just one word.
      else
      {
         relevant_fruits = [];         // Create an empty array if nothing is selected
      }                                // or the selection does not contain any words.
      // Write a dump of entire object relevant_fruits to the output window.
      var_dump(relevant_fruits);
      
      The most efficient solution using function split with the disadvantage that it works only for a selection with exactly one space between the words (fruit names).

      Code: Select all

      var relevant_fruits = UltraEdit.activeDocument.selection.split(" ");
      // There is always an array created by function split. It can be
      // an empty array on nothing selected, but the next function call
      // to remove first element from the array works nevertheless as
      // JavaScript core engine handles this error condition.
      relevant_fruits.splice(0,1);
      // Write a dump of entire object relevant_fruits to the output window.
      var_dump(relevant_fruits);
      
      There are of course lots of other solutions possible which all require more lines of code, but would be executed faster than the first provided solution without the disadvantage of the most efficient second provided solution.
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Mar 30, 2022#3

        Hello Mofi,

        thanks for the thorough answer.
        I really thought, I'm missing something ... (like JS-wise)

        And also thanks for providing an alternative solution.

        Cheers!