Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

How to split using backslash (\)

ramesh_r
Giga Sage

Hi All,

 

I want to split the string using a backslash (\)  and i need value after the backslash  (\)

but I am not getting proper output

Expected output  ==   "_DJ_Ima_KPI-Rap"

var group = "GROUP\_DJ_Ima_KPI-Rap";
var n = group.split("\");
gs.log(n[0]);
gs.log(n[1]);

The expected output is : 

GROUP
_DJ_Ima_KPI-Rap
10 REPLIES 10

Ankur Bawiskar
Tera Patron

Hi,

this should work

during testing you should escape the slash with 1 more slash character so that it is recognised

var group = "GROUP\\_DJ_Ima_KPI-Rap";

gs.info(group);

var n = group.split("\\");

gs.info(n.length);

gs.info(n[0]);
gs.info(n[1]);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

 

This is hard coded value  it should be with single \ only "GROUP\_DJ_Ima_KPI-Rap";

help me with single slash \ 

Thanks

Ramesh

Hi Ramesh,

it won't work with hard-coded value in background script since the string should recognize the slash character so in order for that to work you need to escape that character

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Willem
Tera Sage

"\" is used to escape characters. Is the group value a hard coded string like above? Or is it a variable?

If so you can use:

var n = group.split("\\")

If it is hard coded use this:

var group = "GROUP\\_DJ_Ima_KPI-Rap";
var n = group.split("\\");
gs.print(n[0]);
gs.print(n[1]);