1 and 2 are ok. But the last one has me stumped; the file would be renamed to tes.3. When I try the RegEx in online testers, everything works as expected.
Can anyone explain and/or provide a solution?
Thanks a lot!
MartO
I agree that scripting is not needed for this simple purpose - but this is just a small part of my script.
Obviously your regex is more elegant than mine and I can confirm that it works if used like you suggest. However, in my script it still fails: There's a match for the third file which now gets renamed to "test3" (no extension). What's going on? Isn't that result just wrong? Does the regex engine in the script work differently from the one in the rename dialog or a rename command?
function OnGetNewName(getNewNameData)
{
var nameString = String(getNewNameData.item);
return nameString.replace(/^([^.]+)\.([^.]+)\.bak$/i, "$1.$2");
}
Same thing but making the regexp object explicit:
function OnGetNewName(getNewNameData)
{
var nameString = String(getNewNameData.item);
var re = /^([^.]+)\.([^.]+)\.bak$/i;
return nameString.replace(re, "$1.$2");
}
If you create the regexp object using the syntax you were using, it seems to go wrong:
function OnGetNewName(getNewNameData)
{
var nameString = String(getNewNameData.item);
var re = new RegExp("^([^.]+)\.([^.]+)\.bak$","i");
return nameString.replace(re, "$1.$2");
}
That turns test3.bak into tes.3, like you were seeing with the exec method and matches array. I don't know why that is. It's something JScript is doing, not Opus. Could be something subtle about the difference between using the /.../flags syntax vs new RegExp("...", "flags"), or might even be a bug in JScript.
Solved it! The problem is that the backslash is an escape character not only in regex but also in a string. Of course... So to enter a real "." character in the regex, first you need to escape it so it's "\." and then you need to escape the backslash as well: "\\."
And lo and behold: It works!
@nodeselect
RENAME PATTERN="*" TO="*"
@script JScript
function OnGetNewName(getNewNameData)
{
var item = getNewNameData.item;
re = new RegExp("^([^.]+)\\.([^.]+)\\.bak$",'i');
matches = re.exec(item.name);
DOpus.Output(item.name + " -> " + matches);
return true;
}
You learn something new -- no, you remember something old every day... Thanks for your input!