Skip to main content
MADink_Designs27
Inspiring
November 18, 2019
질문

Regex: Match Field Name and apply setAction based on number in Naming Convention

  • November 18, 2019
  • 1 답변
  • 4541 조회

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;'
					);
			}
		}

 

 

이 주제는 답변이 닫혔습니다.

1 답변

try67
Community Expert
Community Expert
November 18, 2019

Sure, that's possible. You should read the documentation of the RegExp object in JavaScript to learn how to do it, as well as the match method of the String object.

 

(Edit: The match method is of the String object, not RegExp)

MADink_Designs27
Inspiring
November 19, 2019

Thanks Gilad. I know regex—however I learned in InDesign.

 

I looked into the match method, and I've been using match() primarily name.match() in dozens of JS code for Acrobat.

I did learn that I could use regex directly in the match, such as: match(/.+(\d+)/). I typically utilize the $ symbol then digits for groups in the replace query such as:  edit$1, referencing the first group. What I noticed looking up the match method is some were using brackets and numbers instead of the $ symbol—such as: edit[1].

 

What I'm trying to wrap my head around is referencing the digit in the field getting the action applied using setAction to the corresponding digit for the other fields for display visibility...

MADink_Designs27
Inspiring
November 19, 2019

The match method returns an array of all the matches found, or null if none were found. Hence the use of the square brackets, which is how items in an array are accessed in JS.

Here's an example of how to use it:

 

var s = "name1";
var re = /\d+$/;
var matches = s.match(re);
if (matches) {
	var digits = matches[0];
	app.alert("Matched digits: "+digits,3);
} else app.alert("No matches found.");

 

You can play around with the value of the string (or the regular expression) to see what results it yields.


I'm a little confused on how to apply this, but currently working on a code to try.

I see examples of the app.alert, but that doesn't translate to the type of code I need to write.

Should I abandon the for loop completely and go with just the match method or somehow implement the above into the for loop?