Need help to remove space after comma

Shidhi
Tera Contributor

Hi,

I have an onChange client script on a List collector field based on the selection it will populate those names in another variable.


The names are populated as comma separated values and spaces are coming in between as shown below so I have used the replace function to remove the spaces

Shidhi_0-1728482968603.png

But it removes all the spaces rather than the spaces between names.
eg., Suppose a name X Y, Z so my output is XY,Z but i want it to be X Y,Z

Below is the onChange script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    g_form.clearValue('model_handle_list');
    var apps = g_form.getDisplayValue('spm_test_name').replace(/\s+/g, "");
    //g_form.setValue('model_handle_list', apps);
}

Any help/suggestions.

Thank you!

3 ACCEPTED SOLUTIONS

Bert_c1
Kilo Patron

See the following example:

 

var stringVal = 'X Y, Z';
var arrayValues = stringVal.split(',');
var newStringArray = [];
for (i = 0; i < arrayValues.length; i++) {
	newStringArray[i] = arrayValues[i].trim();
}
var newStringVal = newStringArray.toString();
gs.info('New value = ' + newStringVal);

I'm not sure how you do that with regex.

View solution in original post

SN_Learn
Kilo Patron
Kilo Patron

Hi @Shidhi ,

 

Please try the below:

var strVal = 'X Y Z, Z G';

(function validateModel(inputData) {
    var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
    gs.info(formattedNewVal);
})(strVal);

 

Output:

*** Script: X Y Z,Z G

 

Second Use Case:

var strVal = 'X Y, Z';

(function validateModel(inputData) {
    var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
    gs.info(formattedNewVal);
})(strVal);

 

Output:

*** Script: X Y,Z

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

View solution in original post

Bert_c1
Kilo Patron

In the second Use case I get:

 

var strVal = ' X Y, Z ';             // has leading and trailing space

(function validateModel(inputData) {
    var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
//    var formattedNewVal = inputData.replace(/^\s+|\s+$/g, '');
    gs.info('new val:'+formattedNewVal+'.');
})(strVal);

// results in:
*** Script: new val: X Y,Z .
leading and trailing spaces are not revmoved.

// next test:

var strVal = ' X Y, Z ';

(function validateModel(inputData) {
//    var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
    var formattedNewVal = inputData.replace(/^\s+|\s+$/g, '');
    gs.info('new val:'+formattedNewVal+'.');
})(strVal);

results in:
*** Script: new val:X Y, Z.

with leading and trailing spaces removed.

 

Seem that @Shidhi 

can use:

    var apps = g_form.getDisplayValue('spm_test_name').replace(/^\s+|\s+$/g, '');

to get what is wanted.

View solution in original post

4 REPLIES 4

AshishKM
Kilo Patron
Kilo Patron

Hi @Shidhi ,

Check if there is option to replace ", " with ","

the first one - comma and one space

the second one - only comma

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Bert_c1
Kilo Patron

See the following example:

 

var stringVal = 'X Y, Z';
var arrayValues = stringVal.split(',');
var newStringArray = [];
for (i = 0; i < arrayValues.length; i++) {
	newStringArray[i] = arrayValues[i].trim();
}
var newStringVal = newStringArray.toString();
gs.info('New value = ' + newStringVal);

I'm not sure how you do that with regex.

SN_Learn
Kilo Patron
Kilo Patron

Hi @Shidhi ,

 

Please try the below:

var strVal = 'X Y Z, Z G';

(function validateModel(inputData) {
    var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
    gs.info(formattedNewVal);
})(strVal);

 

Output:

*** Script: X Y Z,Z G

 

Second Use Case:

var strVal = 'X Y, Z';

(function validateModel(inputData) {
    var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
    gs.info(formattedNewVal);
})(strVal);

 

Output:

*** Script: X Y,Z

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Bert_c1
Kilo Patron

In the second Use case I get:

 

var strVal = ' X Y, Z ';             // has leading and trailing space

(function validateModel(inputData) {
    var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
//    var formattedNewVal = inputData.replace(/^\s+|\s+$/g, '');
    gs.info('new val:'+formattedNewVal+'.');
})(strVal);

// results in:
*** Script: new val: X Y,Z .
leading and trailing spaces are not revmoved.

// next test:

var strVal = ' X Y, Z ';

(function validateModel(inputData) {
//    var formattedNewVal = inputData.replace(/\s*,\s*/g, ',');
    var formattedNewVal = inputData.replace(/^\s+|\s+$/g, '');
    gs.info('new val:'+formattedNewVal+'.');
})(strVal);

results in:
*** Script: new val:X Y, Z.

with leading and trailing spaces removed.

 

Seem that @Shidhi 

can use:

    var apps = g_form.getDisplayValue('spm_test_name').replace(/^\s+|\s+$/g, '');

to get what is wanted.