How to split using backslash (\)

ramesh_r
Mega 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
Giga 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
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
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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader