- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2023 05:20 PM
Hello,
I have a string contains "abc\1234"
I would like to extract from the back slash and get the number only such as "1234"
Can someone please help? Thank you
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2023 05:30 PM - edited 05-25-2023 05:33 PM
Try this one
var string = "abc\1234";
var newString =string.replace('abc\','')
gs.info(newString);
Log:
*** Script: 1234
Please mark helpful if correct, thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2023 06:51 PM
Hi Zack,
I'm trying to test the script in background script, but getting the following error:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2023 09:10 PM
Hello @Annie10,
Please try the below script.
In the script orig_string (original string) contains \\ as an escape sequence (additional / for the ServiceNow to understand that next / has no special meaning).
IndexOf function is used to identify the position of \ in the string and slice method is used to retrieve the text that is available after /.
var orig_string = "abc\\1234";
var position= orig_string.indexOf("\\")+1;
var ext_string = orig_string.slice(position);
gs.info(ext_string);
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2023 09:34 PM
Hi @Arun_S1
My original string contains double black slash "abc\12345"
I was trying to get the number after the back slash using transform map script like this, but it did not work:
return source.u_name.toString().replace(/(\d+)/g, "");