User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Help with writing and running scripts
4 posts Page 1 of 1
This regular expression works in the find replace dialog but does not work in the script.
What am I doing wrong in the script?
The intended find string is:
(2017-06)(.*)
the intended replace string is:
\1 BLAH \2

Many thanks for any reply

Code: Select all
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.findReplace.regExp = true;
while (UltraEdit.activeDocument.findReplace.replace("(2017-06)(.*)","\1 BLAH \2"))
UltraEdit.activeDocument.top();


Sample source file to execute the script on:
Code: Select all
2017-06-19z
2017-06-19y
2017-06-19x
2017-06-19w
2017-06-19v


The intended effect is:
Code: Select all
2017-06 BLAH -19z
2017-06 BLAH -19y
2017-06 BLAH -19x
2017-06 BLAH -19w
2017-06 BLAH -19v
2slouw
You should to escape "\" with \ in findReplace.replace and use replace all instead of while loop

Code: Select all
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.findReplace.regExp = true;
UltraEdit.activeDocument.findReplace.replaceAll = true;
UltraEdit.activeDocument.findReplace.replace("(2017-06)(.*)","\\1 BLAH \\2");
It's impossible to lead us astray for we don't care even to choose the way.
Ovg posted already the right solution. In case of you are interested in why each backslash in a Perl regular expression search/replace string must be additionally escaped with a backslash when used in a script defined in a JavaScript string, read point 2 - Backslashes in strings are not escaped - in scripts announcement post List of UltraEdit / UEStudio script commands and most common mistakes.
Best regards from Austria
@Mofi and @Ovg
Superb! Wonderful! Thank you.
I did read that post Mofi but I did not click :)
4 posts Page 1 of 1
cron