Strange behaviour of Perl RegExp in Javascript engine

Strange behaviour of Perl RegExp in Javascript engine

3
NewbieNewbie
3

    Jun 23, 2008#1

    I'm using UE 13.10a+1.

    Glad to have got a powerfull scripting engine - I never liked UE Macros as "programming language" - I've found that the Perl regular expression engine within Javascript behaves very strange.
    First I've wondered, why there is an extra object/method to use:

    Code: Select all

    UltraEdit.activeDocument.findReplace.regExp = true;
    UltraEdit.activeDocument.findReplace.find
    while Javascript implementations in browsers or MS-Scripting host since years allow RegExp objects to be created with

    Code: Select all

    new RegExp("expr","flags")   or with   /expr/flags

    I've even tried them, without error messages, but also without success. :?

    Then I tried to write a little script to create proper idented javascript object from my Firefox "sessionstore.js", where the identation should be increased after the characters "{[(" and decreased before "}])". The first try was:

    Code: Select all

    UltraEdit.columnModeOff;
    UltraEdit.hexOff;
    UltraEdit.perlReOn;
    UltraEdit.activeDocument.findReplace.regExp = true;
    UltraEdit.activeDocument.findReplace.find("\[|\]");
    
    It found only occurences of "|" :o

    Next I tried character classes:

    Code: Select all

    UltraEdit.activeDocument.findReplace.find(/[\[\]{}{)]/);
    did nothing - and no error.

    Next try:

    Code: Select all

    UltraEdit.activeDocument.findReplace.find("[\[\]{}()]");
    told me: "You have entered an invalid expression".
    The identical regular expression entered in the Find dialog (with Perl regExp enabled) WORKS 8O

    Next try:

    Code: Select all

    UltraEdit.activeDocument.findReplace.find("[\[{}()]");
    worked as expected.

    Next try:

    Code: Select all

    UltraEdit.activeDocument.findReplace.find("[\]{}()]");
    worked as expected.

    Last try:

    Code: Select all

    UltraEdit.activeDocument.findReplace.find("[\[\]]");
    did nothing - and no error. :evil:

    Anything I've overseen?

    344
    MasterMaster
    344

      Jun 23, 2008#2

      You forgot to escape the backslash for Javascript.
      I'm in a hurry, but a fast example:

      Code: Select all

      UltraEdit.activeDocument.findReplace.find("[|\\\\]");
      will finf | and \ as you expected
      Normally using all newest english version incl. each hotfix. Win 10 64 bit

      3
      NewbieNewbie
      3

        Jun 23, 2008#3

        Hi Bego,
        thanks for your feedback. But I do not want to find | or \, but brackets/braces. The "|" was ment as alteration, the "\" to quote the characters with special meaning in RegExp i.e. "\[" and "\]" in classes, "\(" and "\)" outside!
        But thanks anyway ...

          Jun 23, 2008#4

          Thanx again,

          you pointed met into the right direction.
          The correct syntax is:

          Code: Select all

          UltraEdit.activeDocument.findReplace.find("[\\[\\]{}()]");
          The "\\" is never not necessary in Perl or in JS when using the "/" delimiter, indicating that you work on a RegExp and not a plain text string.
          I would prefer syntax from my first post with ".find( /[\[\]{}()]/ ), it indicates directly that I want to have a RegExp. :(

          Regards, Ulf

          119
          Power UserPower User
          119

            Jun 23, 2008#5

            heilu wrote:The "\\" is never not necessary in Perl
            Yes, well, you aren't programming in Perl. Regular expressions aren't first-class objects in JavaScript.
            heilu wrote:I would prefer syntax from my first post with ".find( /[\[\]{}()]/ ), it indicates directly that I want to have a RegExp.
            Sounds good to me. Send a feature request to IDM.

            344
            MasterMaster
            344

              Jun 23, 2008#6

              @heilu: Nice to hear that I pointed you in the right direction by answering your mail 50% correctly ;-)
              Now I got more time to read...

              mj: You got my vote too! It is boring to test a regexp and then have to convert it to "script-style" :-/

              greetz, Bego
              Normally using all newest english version incl. each hotfix. Win 10 64 bit

              262
              MasterMaster
              262

                Jun 24, 2008#7

                And my vote too for the feature request.

                I'm also tired of this escaping \ in regexp when using UE. You'll see me use this trick in the scripts I post in this forum to ease the copy and pasting of Regexp's:

                Code: Select all

                UltraEdit.perlReOn();
                
                // Assign a helper prototype function for the RegExp object:
                RegExp.prototype.toUEregexp = function() { return this.toString().replace(/^\/|\/$/g, ""); }; /* remove starting and ending / as UE does not recognize these for regexp */
                
                // setup a regexp variale:
                var myPerlRegExp = /[\[\]{}()]/;
                
                // then use this in the UE find together with the prototype function:
                UltraEdit.activeDocument.findReplace.find(myPerlRegExp.toUEregexp);

                16
                Basic UserBasic User
                16

                  Jun 24, 2008#8

                  mjcarman wrote:Regular expressions aren't first-class objects in JavaScript.
                  They are in some implementations