Javascript split with escape character

VivekSattanatha
Mega Sage
Mega Sage

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.

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

Thanks Ankur, I was thinking only with Split maybe replace also would help here. I will try and let you know tomorrow.

 

Regards,

Vivek

Soleil Levant
Tera Contributor

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("__");


}