Transform Script: Split String Value with Backslash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-10-2020 06:13 AM
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?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-10-2020 06:25 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-10-2020 06:27 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-10-2020 06:39 AM
Hi Thomas,
Please try to print the incoming value first in log and check.
Regards
Ankur
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-10-2020 07:30 AM
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