Using replace in run script is not working

phoang
Kilo Expert

Hi,

I'm trying to replace spaces in my string with a comma within a Run Script in my workflow, but I keep getting 'undefined'.  This is what I've tried:

var copyNona3 = copyNona.replace(/,\s*$/, "");

var copyNona3 = copyNona.replace(/\s/g, ',');

var copyNona3 = copyNona.replace(/\s/g, ',');

But none of that worked.  I also joined a split().join() but that didn't work either.  

Can you use replace in a Run Script of a workflow?

 

Thanks.

Phuong

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

If you are getting undefined while calling replace, then copyNona might not be a string. Convert to string and then check it.

If you are getting undefined while printing copyNona3, thjen try this code

var copyNona2 = "Testing";
var copyNona3 = copyNona2.replace(/\s/g, ',');

Mark the comment as a correct answer and helpful if it helps.

View solution in original post

7 REPLIES 7

Elijah Aromola
Mega Sage

Are you sure there are spaces in the string? The following works fine in the console:

var copyNona3 = copyNona.replace(/\s/g, ',');

Yes I've tried spaces and returns and getting undefined.  The log shows the data from variable and it's how I entered but then after using the replace it's undefined.  My full script:

 

var copyNona = current.variables.asset_copy_non;
gs.log('copyNona: ' + copyNona);
var copyNona1 = copyNona.replace(/\s/g, ',');
//var copyNona3 = copyNona.replace(/,\s*$/, "");
gs.log('copyNona1: ' + copyNona1);
current.variables.nonSL=copyNona1;

Steven Parker
Giga Sage

This works fine testing as a background script:

var copyNona = "This is a Test";
var copyNona3 = copyNona.replace(/\s/g, ',');
gs.print(copyNona3);

Does copyNona have spaces in it or even data in it?  Usually undefined means nothing is in the variable or maybe it doesn't have spaces.  You should be able to do that in a run script in a workflow.


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

The log shows data from the variable:

 

copyNona: test1 test2

 

But still shows undefined.

 

My full script:

var copyNona = current.variables.asset_copy_non;
gs.log('copyNona: ' + copyNona);
var copyNona1 = copyNona.replace(/\s/g, ',');
//var copyNona3 = copyNona.replace(/,\s*$/, "");
gs.log('copyNona1: ' + copyNona1);
current.variables.nonSL=copyNona1;