Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

Hi Willem,

 

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

help me with single slash \ 

Thanks

Ramesh

It will not accept "GROUP\_DJ_Ima_KPI-Rap" as a hardcoded string. If you retrieve it dynamically you can use:

Where groupname_field is for example a name field.

group = decodeURIComponent(groupname_field);
var n = group.indexOf("_");
gs.info(group.substr(0, n));
gs.info(group.substr(n));

 

Or if you want to hard code it, do it like this:

group = decodeURIComponent("GROUP\_DJ_Ima_KPI-Rap");
var n = group.indexOf("_");
gs.info(group.substr(0, n));
gs.info(group.substr(n));

AbhishekGardade
Tera Sage

Hello Ramesh,

 

Try this,


var str = 'first\\last';//returns 'first\last'
var arr = str.split('\\');
gs.log(arr[1]);//returns last

Thanks,
Abhishek Gardade

Thank you,
Abhishek Gardade

Willem
Giga Sage

Hope you are doing well.

Is your question resolved? Or do we need to follow-up on this?

Please mark this answer as correct if it solves your question. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

Kind regards,

Willem

Ankur Bawiskar
Tera Patron

@ramesh.r 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark my answer as correct & helpful to close the thread.

If not, please let us know if you need some more assistance.

Regards
Ankur

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