Transform Script: Split String Value with Backslash

Thomas_J_C
Mega Expert

Currently attempting to split some source string values for example:

"EXAMPLE-US-WEST\DC91P-NS-AP74" (I'd like to get the SECOND half of the string "DC91P-NS-AP74" from the value)

My current code is:

var bios = source.u_biosName.toString().split(/\\/)[1];

However, the string value comes up as undefined and if I put the string index to [0], the log message will come up as "EXAMPLE-US-WESTDC91P-NS-AP74" and will just replace the backslash. 

Any ideas how to split this source string values to get the second half of the value after the backslash?

 

 

8 REPLIES 8

Brad Bowman
Kilo Patron
Kilo Patron

The issue is not the backslash alone, rather the backslash combined with the next letter which is an escape sequence.  For your example string, this will work

var bios = source.u_biosName.toString().replace("\D", "\\D").split("\\")[1];

Unfortunately you'll have to do the same for every letter that can/does appear after the \.  If you can manipulate the source data prior to import, you won't need the .replace at all if you change the \ to \\, or just about any other character.

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

When the original string is being processed, it is ignoring the \ as escape character, so at the time pf slipt it does get the char at all and that is why the whole string goes on [0] position in the array and there is no [1]

 Your code would run perfectly if the incoming string would be 

'EXAMPLE-US-WEST\\DC91P-NS-AP74';

-Anurag

-Anurag

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Thomas,

Please try to print the incoming value first in log and check.

Regards
Ankur

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

Jaspal Singh
Mega Patron
Mega Patron

Hi Thomas,

 

As Anurag advised since '\' is being sent it is not being processed as it is an escape character. Incoming string then needs to have a format that may be '\\' or '||'. Else, if you confirm that the position where the string needs to be extracted from would always be 16th then you can use script as below.

 

var abc=source.u_biosName.toString();
var bios=abc.substr('15');

But this would work only if there is a confirmation that the 16th character would string from where the remaining data is to be retrieved.

 

Thanks,

Jaspal Singh