Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to split an array with comma when values itself contains comma?

Ankita Kolhe
Tera Contributor

Hi Community,

 

There is one field of type 'glide-list' & I want to split them with comma but the issue is value itself contains comma.

Please see the screenshot below:-

 

AnkitaKolhe_1-1725597965875.png

 

var grp=gr.groupings.getDisplayValue()).split(",");

 

It display value as:

 

['Entity Update - Norway', 'Entity Update - Sweden', 'Latvia & Lithuania'] which means it's splitting array as 

 

grp[0]=Entity Update - Norway

grp[1]=Entity Update - Sweden

grp[2]=Latvia & Lithuania

 

I want it as below:-

 

grp[0]=Entity Update - Norway

grp[1]=Entity Update - Sweden, Latvia & Lithuania

 

i.e. It should always start with 'Entity Update' and end before ',Entity Update'.

 

I tried multiple regex methods & checked on community but didn't find any solution.

 

Could someone please help me on this?

 

Thanks in advance!!

 

1 ACCEPTED SOLUTION

HIROSHI SATOH
Mega Sage

Try it!

var grp = gr.groupings.getDisplayValue();  // Get the value of the glide-list
var regex = /, (?=Entity Update)/;  // Regular expression to split at ", " only when followed by "Entity Update"

var splitGrp = grp.split(regex);  // Use the regex to split the string

// Output the split values
for (var i = 0; i < splitGrp.length; i++) {
    gs.info("grp[" + i + "]=" + splitGrp[i]);
}

 Output

grp[0] = Entity Update - Norway
grp[1] = Entity Update - Sweden, Latvia & Lithuania

View solution in original post

2 REPLIES 2

HIROSHI SATOH
Mega Sage

Try it!

var grp = gr.groupings.getDisplayValue();  // Get the value of the glide-list
var regex = /, (?=Entity Update)/;  // Regular expression to split at ", " only when followed by "Entity Update"

var splitGrp = grp.split(regex);  // Use the regex to split the string

// Output the split values
for (var i = 0; i < splitGrp.length; i++) {
    gs.info("grp[" + i + "]=" + splitGrp[i]);
}

 Output

grp[0] = Entity Update - Norway
grp[1] = Entity Update - Sweden, Latvia & Lithuania

Hello @HIROSHI SATOH ,

Thank you so much for your instant response 🙂 It's working now.

You are the saviour.