Regex: Match Field Name and apply setAction based on number in Naming Convention
How can I write a JavaScript utilizing the setAction method on 30 sets of fields with the same numeric naming convention?
I often utilize a for loop to match fieldnames and set Acrobat JS methods, but I'd love to learn how to utilize a find/change regex method in this manner.
I don't know if it's possible, but I'd like to match the "hover_\d+" field and run one setAction to show/hide fields, etc.
In other words, the digit of whatever field hover is, would be the same as the rest of the fields 1-30.
I'd love to be able to write it something like: "hover_(\d+)" ==> edit$1, copy$1, trash$1, etc. where I'm backreferencing whatever digit the boolean logic is finding in the name.match() code.
I could write this out 30 times, easy—but am wondering if there's a quicker, cleaner way of doing so.
Perhaps an array with just 1-30 and reference the array in the display script?
Perhaps instead of focusing on the "hover field", focus on the numbers whereas the regex would be: /.+1/ - /.+30/; or something of the such?
var cHover1 = this.getField("hover_1");
var wHover1 = this.getField("hover_wh1");
var edit1 = this.getField("edit1");
var copy1 = this.getField("copy1");
var trash1 = this.getField("trash1");
var close1 = this.getField("close1");
for (var i = 0; i < this.numFields; i++) {
var fname = this.getNthFieldName(i);
var f = this.getField(fname);
hoverC = /hover_\d+/;
hoverW = /hover_wh\d+/;
editB = /edit\d+/;
copyB = /copy\d+/;
trashB = /trash\d+/;
closeB = /close\d+/;
if(f.name.match(hoverC)) {
f.setAction("MouseEnter",
'cHover1.display = display.hidden;' +
'\rwHover1.display = display.noPrint;' +
'\redit1.display = display.noPrint;' +
'\rcopy1.display = display.noPrint;' +
'\rtrash1.display = display.noPrint;' +
'\rclose1.display = display.noPrint;'
);
}
}
