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

Help with s.replace

Explorer ,
Jan 18, 2024 Jan 18, 2024

Ok so I am concatentating strings together for a full address in two fields. First field is fine, it uses all the fields and works fine. However, in the second address field it will say "SAME AS BLOCK 3" by default but it has my extra commas from my script in it. I am trying s.replace but I don't know what I'm doing and I can't really find anything that solves my problem. I'm not even sure if that is the right code I should be using.
Can someone walk me through my code so I can learn?

var s = this.getField("ADDRESS.1").valueAsString + ", "
 + this.getField("CITY.1").valueAsString + ", "
 + this.getField("COUNTY.1").valueAsString + ", "
 + this.getField("STATE.1").valueAsString + ", "
 + this.getField("COUNTRY.1").valueAsString + ", "
 + this.getField("ZIP.1").valueAsString 
event.value = s.replace(/^, |, $/g, "", ', , ');

 OUTPUT: 

SAME AS BLOCK 3, , , , 

 

What I'm looking for:

SAME AS BLOCK 3

TOPICS
How to , JavaScript , PDF forms
802
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Jan 18, 2024 Jan 18, 2024

To be honest, this is a pretty bad way of doing it. I would do it like this:

 

var fields = ["ADDRESS.1", "CITY.1", "COUNTY.1", "STATE.1", "COUNTRY.1", "ZIP.1"];
var values = [];
for (var i in fields) {
	var f = this.getField(fields[i]);
	var v = f.valueAsString;
	if (v!="") values.push(v);
}
event.value = values.join(", ");

View solution in original post

Translate
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 ,
Jan 18, 2024 Jan 18, 2024

Drop the markers for the first and last characters in your RegExp (^ and $, resp.), and it will work.

Translate
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 ,
Jan 18, 2024 Jan 18, 2024

Also, the replace function only takes two input parameters. What's the third one for?

Translate
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
Explorer ,
Jan 18, 2024 Jan 18, 2024

Like this?

Old:

event.value = s.replace(/^, |, $/g, "", ', , ');

 

New:

event.value = s.replace(/, |,/g, "", ', , ');
quote

Also, the replace function only takes two input parameters. What's the third one for?


By @try67

I have no idea even what the 1st and 2nd do. To me it looks like 5 different parameters. I'm used to programming excel. I found it and was just trying to figure it out. I'm not even sure whats going on in that. I've been trying to find an answer on parameters since last night, I just don't know enough to be able to convey what I'm looking for.

Translate
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 ,
Jan 18, 2024 Jan 18, 2024

To be honest, this is a pretty bad way of doing it. I would do it like this:

 

var fields = ["ADDRESS.1", "CITY.1", "COUNTY.1", "STATE.1", "COUNTRY.1", "ZIP.1"];
var values = [];
for (var i in fields) {
	var f = this.getField(fields[i]);
	var v = f.valueAsString;
	if (v!="") values.push(v);
}
event.value = values.join(", ");
Translate
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
Explorer ,
Jan 18, 2024 Jan 18, 2024
quote

To be honest, this is a pretty bad way of doing it. I would do it like this:

 

 

var fields = ["ADDRESS.1", "CITY.1", "COUNTY.1", "STATE.1", "COUNTRY.1", "ZIP.1"];
var values = [];
for (var i in fields) {
	var f = this.getField(fields[i]);
	var v = f.valueAsString;
	if (v!="") values.push(v);
}
event.value = values.join(", ");


By @try67

 

Ok so this retrieves the values of the fields, puts it in order, places the comma, but if blank it pushes nothing into the string and leaves no joining comma. I had to look up values.push to gleen info from that.

Translate
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 ,
Jan 18, 2024 Jan 18, 2024

I can confirm that you have correctly described exactly how the script works. A value is pushed onto the "values" array only if it is non-blank. 

Is this the result you wanted? Was there another question?

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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
Explorer ,
Jan 18, 2024 Jan 18, 2024
LATEST

No thats it. I'm trying to learn. 

Translate
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