Skip to main content
DarthTormaim
Known Participant
January 18, 2024
Answered

Help with s.replace

  • January 18, 2024
  • 1 reply
  • 1166 views

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

This topic has been closed for replies.
Correct answer try67

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(", ");

1 reply

try67
Community Expert
Community Expert
January 18, 2024

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

try67
Community Expert
Community Expert
January 18, 2024

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