Strange behaviour of Perl RegExp in Javascript engine

Find, replace, find in files, replace in files, regular expressions

Strange behaviour of Perl RegExp in Javascript engine

Postby heilu » Mon Jun 23, 2008 12:29 pm

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?
heilu
Newbie
 
Posts: 3
Joined: Mon Jun 23, 2008 10:17 am
Location: Germany

Re: Strange behaviour of Perl RegExp in Javascript engine

Postby Bego » Mon Jun 23, 2008 2:30 pm

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
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Strange behaviour of Perl RegExp in Javascript engine

Postby heilu » Mon Jun 23, 2008 2:54 pm

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 ...
heilu
Newbie
 
Posts: 3
Joined: Mon Jun 23, 2008 10:17 am
Location: Germany

Re: Strange behaviour of Perl RegExp in Javascript engine

Postby heilu » Mon Jun 23, 2008 3:03 pm

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
heilu
Newbie
 
Posts: 3
Joined: Mon Jun 23, 2008 10:17 am
Location: Germany

Re: Strange behaviour of Perl RegExp in Javascript engine

Postby mjcarman » Mon Jun 23, 2008 3:18 pm

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.
User avatar
mjcarman
Power User
Power User
 
Posts: 123
Joined: Thu Feb 10, 2005 12:00 am

Re: Strange behaviour of Perl RegExp in Javascript engine

Postby Bego » Mon Jun 23, 2008 3:38 pm

@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
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Strange behaviour of Perl RegExp in Javascript engine

Postby jorrasdk » Tue Jun 24, 2008 2:43 am

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);
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Strange behaviour of Perl RegExp in Javascript engine

Postby SamHasler » Tue Jun 24, 2008 4:35 am

mjcarman wrote:Regular expressions aren't first-class objects in JavaScript.

They are in some implementations
User avatar
SamHasler
Basic User
Basic User
 
Posts: 16
Joined: Mon Aug 16, 2004 11:00 pm
Location: UK


Return to Find/Replace/Regular Expressions