• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Engaged ,
Nov 18, 2019 Nov 18, 2019

Copy link to clipboard

Copied

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

 

 

TOPICS
Acrobat SDK and JavaScript

Views

2.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 18, 2019 Nov 18, 2019

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 19, 2019 Nov 19, 2019

Copy link to clipboard

Copied

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 19, 2019 Nov 19, 2019

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 19, 2019 Nov 19, 2019

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 19, 2019 Nov 19, 2019

Copy link to clipboard

Copied

You can still use the for loop. The alert is just to display the results of the match method. You don't need to use it in your code. What you need to do is use the equivalent of the digits variable I've defined in my code in order to set the action of your fields. Note that you have to specify the full field name in it, though. You can't use names of variables you've defined elsewhere (unless you've defined them at a doc- or folder-level script).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 20, 2019 Nov 20, 2019

Copy link to clipboard

Copied

I typically make variables document level to avoid most issues. 

I'm just trying to get the code you sent to work before I can tackle the logic for my purposes. I finally got back to this today—albiet late in the day.

I think Im beginning to understand that I'm referencing a specific field name and the match method isn't concatenating the field then the regex number separately...It's specifying a field name and the match method is looking for other fields with the same pattern of the "s" variable with the same regex value on the digits.

My current issue in this step of learning is getting your code to work. 

It keeps popping up with a type error saying s.match(re); isn't a function...

//try67's code example_marc edit
	var s = this.getField("hover_1");
	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.");
	}
TypeError: s.match is not a function
4:Console:Exec
undefined

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 20, 2019 Nov 20, 2019

Copy link to clipboard

Copied

The "s" variable needs to be a String object, not a Field object.

Change this line:

var s = this.getField("hover_1");

To:

var s = this.getField("hover_1").valueAsString;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 21, 2019 Nov 21, 2019

Copy link to clipboard

Copied

That's what's confusing me. What I'm trying to do is a field object. I did as suggested, and it didn't come up with an error but it came up with no matches found.

 

I'm not trying to find matches for values inside the field. I bought your regex find/change script for that...I'm trying to find matches in the field name.

 

The script should have found 10 matches.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 21, 2019 Nov 21, 2019

Copy link to clipboard

Copied

LATEST

Could you email me the file? I'll help you further privately.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines