
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2018 04:40 AM
I have a string like below which is coming from script.
("Table1"),('Table2'),('Table3'),("Table4"),("Table4")
I want to do a split in the string and make it like below
Table1,Table2,Table3,Table4
How do I achieve this. I have read somewhere we can achieve this by regex split but I am not a expert in making regular expression. Does anyone have any idea how to make this possible.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2018 04:56 AM
Hi Vivek,
First of all you need to ensure the double quotes and single quotes i.e. Values such as Table1, Table2 etc are in double quotes are single quotes
Either all value to be in double quotes or single quotes
Example: In this values are wrapped around double quotes
var data = '("Table1"),("Table2"),("Table3"),("Table4"),("Table4")';
var val = data.split(",");
for(var i=0;i<val.length;i++)
{
var incomingVal = val[i];
incomingVal = incomingVal.replace("(\"","");
incomingVal = incomingVal.replace("\")","");
gs.print(incomingVal);
}
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
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
‎06-05-2018 04:56 AM
Hi Vivek,
First of all you need to ensure the double quotes and single quotes i.e. Values such as Table1, Table2 etc are in double quotes are single quotes
Either all value to be in double quotes or single quotes
Example: In this values are wrapped around double quotes
var data = '("Table1"),("Table2"),("Table3"),("Table4"),("Table4")';
var val = data.split(",");
for(var i=0;i<val.length;i++)
{
var incomingVal = val[i];
incomingVal = incomingVal.replace("(\"","");
incomingVal = incomingVal.replace("\")","");
gs.print(incomingVal);
}
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
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
‎06-05-2018 08:13 AM
Thanks Ankur, I was thinking only with Split maybe replace also would help here. I will try and let you know tomorrow.
Regards,
Vivek
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2018 03:47 AM
The solution, I have the same problem a string with \ and the need is to extract the second part
var Mytext = yourstring; // (current_Field or a source.field.......)
var array = [];
array.push(Mytext);
var myTextOk = JSON.stringify(array).replace(/\\/g,"__");
myTextOk= myTextOk.replace(/\\/g,"__");
var myTexta = JSON.parse(myTextOk);
var myList = obj[0].split("__");
}