- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2020 08:55 AM
Hi All,
I am trying to split/trim/slice the following string "aaaaaaa-test1:\ritmtest123_testgrow_test\testtest" and I am trying to get value "test"(3rd value) from "ritmtest123_testgrow_test".
I Tried to Split with "\" but dint work so tried Trim which didnt work either.
Can someone please help with extracting the above mentioned string from the given string.
Thanks
Solved! Go to Solution.
- 28,995 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2020 09:29 AM
Hi All,
Thanks for all your response, but the path which user provide will not be consistent, so I used the indeOf() to search for the word to achieve what I need.
Again Thanks for all your responses.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2020 09:07 AM
Hi ursnani,
The easiest way would be to split the string by('\') then third element would be your sting.
use :
var str = 'aaaaaaa-test1:\ritmtest123_testgrow_test\testtest';
var b = str.split('\');
var c = b[2];
gs.print(c);
c would retunr you the testtest
Mark helpful and correct if it helps.
Thanks,
CB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2020 09:30 AM
Hey ursnani,
try like this:
const originalString = "How are you?";//enter your string here
// Split string by whitespace character
const splitString = originalString.split(" ");
var s=spliString[2];
gs.print(s);
also refer the thread:
https://community.servicenow.com/community?id=community_question&sys_id=99ef87a5dbdcdbc01dcaf3231f96...
Mark Correct and Helpful if you find my response worthy!!!
Best Regards,
Namrata.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2020 10:04 AM
Hi ursnani,
The above string contain escape characters so it will the value of those characters in java script stings.so to
for example : if it contains \n , or \t or \r it will repalce with the special charactes so you have to check if the string has those characters then do substring to get the values.
For the above example the code should be :
var str = "aaaaaaa-test1:\ritmtest123_testgrow_test\testtest"; // Enter sting
var patt1 = /\t/; //Checking \t
var result = str.search(patt1); //return the position of \t in the sting
//In above the position is 39 so we have used that position and till the last lenght
var sub = str.substring(result,str.length);
var final_strng = 't'+ sub; //Gives you the third element value which is testtest
Escape characters document
https://www.w3schools.com/js/js_strings.asp
Mark helpful and correct if it helps
Thanks,
CB

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2020 10:56 AM
Hi
If you want to extract "test"(3rd value) from "ritmtest123_testgrow_test".
try this in background script for quick output
var str = "aaaaaaa-test1:\ritmtest123_testgrow_test\testtest"; // Enter sting
var patt1 = /\t/;
var patt2 = /\r/;
var end = str.search(patt1); //return the position of \t in the sting
var start = str.search(patt2); //return the position of \r in the sting
var middleString = str.substring(start, end); // splicing the middle portion of the string
var sp = middleString.split('_'); // splitting on the basis of underscore '_' to get the third value from middle portion of stirng
gs.print(sp[2]); // it will print the desired output.
// return sp[2];
output:
Please mark my response ACCEPTED & HELPFUL if it resolves your issue.
Thanks & Regards,
Sharjeel
Muhammad