How to use the String Object methods with Regular Expression in Ultraedit?

How to use the String Object methods with Regular Expression in Ultraedit?

15
Basic UserBasic User
15

    Apr 29, 2009#1

    Dear forum readers,

    This is my third question in short time. Please believe me when I say I am making an effort to RTFM ! I am just not able to find the answers.

    I would like to use a Regular Expression to test a string in an if statement :

    Code: Select all

    if (UltraEdit.activeDocument.selection == RegExp(<mystring>+OneRandomCharacter) { ...
    // So something like this :
    if (UltraEdit.activeDocument.selection == RegExp("12?") { ...
    where I use the ? as a replacement char for any character. But I cannot make the test work.

    Am I totally out on the wrong playground when I imagine that this is possible with regular expression ?

    Edited: OK. I worked around it by using

    Code: Select all

    		UltraEdit.activeDocument.startSelect();
    		UltraEdit.activeDocument.key("LEFT ARROW");
    
    ... to increase the selection by one. In this way I also ignore the last character in my original selection of three characters.

    I would still like to know how to test a string ( true or false output ) using a regular expression.

    Kind regards

    Jørn

    Using UltraEdit 15.00.0.1043 on XP SP3.

    262
    MasterMaster
    262

      Apr 30, 2009#2

      Are you sure you are reading the right manuals ? You have moved beyond UltraEdit manuals when working with strings in JavaScript. So be sure to read a JavaScript manual. In the sticky of this scripts forum you'll find links to a couple of javaScript tutorials and references.

      But here are few ways to test your string against a Regexp:

      Code: Select all

      // Define Regexp object. Uses Perl Regular Expression Syntax:
      var myRegexp = /12./;
      
      if (myRegexp.test(UltraEdit.activeDocument.selection)) { UltraEdit.outputWindow.write("hit using test"); }
      
      if (UltraEdit.activeDocument.selection.match(myRegexp)) { UltraEdit.outputWindow.write("hit using match"); }
      
      var regExpMatchValue = myRegexp.exec(UltraEdit.activeDocument.selection);
      if (regExpMatchValue) { UltraEdit.outputWindow.write("hit using exec - matched value="+regExpMatchValue); }
      To learn more goto W3Schools - JavaScript RegExp Object Reference

      3
      NewbieNewbie
      3

        May 11, 2009#3

        Hi, could anyone tell me how to use the String Object methods with Regular Expression in Ultraedit?
        Does it support myString.split(myRegexp),myString.search(myRegexp) and myString.search(myRegexp)?
        Please give me some examples, thanks.

          May 12, 2009#4

          Thanks jorrasdk for your post.
          I try split() before, it won't success running.
          In your recommended post above, the string object is under UltraEdit. How about general string?

          var str = "Peter Lois Chris Meg Stewie";
          var pattern = /\W+/;
          result = str.split(pattern);

          One more question: Can I use alert() function in UltraEdit?

          262
          MasterMaster
          262

            May 12, 2009#5

            swipe wrote:One more question: Can I use alert() function in UltraEdit?
            Nope, the embedded javascript engine in UE supports the core functionality of JavaScript 1.7. alert() is not core. Use UltraEdit.messageBox("message", "title"); instead.
            swipe wrote:I try split() before, it won't success running.
            In your recommended post, the string object is under UltraEdit. How about general string?
            I don't quite understand. Your example works perfectly. Have a look at the UltraEdit output window (View - Views/Lists - Output Window) for any errors.

            3
            NewbieNewbie
            3

              May 13, 2009#6

              Thanks, I got my point.
              The following code can work perfectly:

              var str = "Peter Lois Chris Meg Stewie";
              var pattern = /\W+/;
              result = str.split(pattern);


              But real question is that why we need another different form to use the Regular Expression.
              We need to define a tricky code firstly, what is reason behind it?
              RegExp.prototype.toUEregexp = function() { return this.toString().replace(/^\/|\/$/g, ""); }; /* remove starting and ending slashes */
              After that, we can use the normal Regular Expression
              UltraEdit.activeDocument.findReplace.find(searchRegexp.toUEregexp());

              Why can we use it directly?

              I am really confused. My original test is the following:
              var str = "Peter Lois Chris Meg Stewie";
              var pattern = /\W+/;
              result = str.split(pattern.toUEregexp());


              It fails to execute it.

              262
              MasterMaster
              262

                May 13, 2009#7

                First, UltraEdit.activeDocument.findReplace.find() is UE method that only accepts strings and not Regexp objects. Why? - IDM's choice. Send them a feature request asking that UE's find() method should support Regexp objects.

                So when I have a Regexp object in my script and need to reuse it in the find() method I have to transform it to String using ...toUEregexp().

                Second,
                result = str.split(pattern.toUEregexp());
                is "core" javascript and the split() method need a real Regexp object to recognize it as regexp. So drop the toUEregexp().

                So you have to distinguish between what is core javascript and what is UE methods and properties.