How to remove \$ from JSON String

ajitmundhekar
Tera Expert

Hi Everyone,

I am using SNow Jakarta instance & I am not able to remove '$' from my JSON string. Below is my code snippet:

var response = request.execute();

var getIdeationData = response.getBody();

var reg1 = new SNC.Regex('/u_number/');

getIdeationData = reg1.replaceAll(getIdeationData,'u_trading_partner_id');

getIdeationData.replace(/$/g,'');

PS: getIdeationData is my JSON string having key-value paired data.

In above code, Line 3 & 4 is working as expected. It is successfully replacing all occurrences of u_number with u_trading_partner_id.

But its not removing $. Till now I have tried below options to perform this task but hard luck...

getIdeationData.replace(/\$/g,'');

getIdeationData.replaceAll(/$/g,'');

getIdeationData.replace(/[$]/g,'');

getIdeationData.replace(/[\$]/g,'');

Thanks in advance!

1 ACCEPTED SOLUTION

Nate23
Mega Guru

have you tried toString() ? like:



getIdeationData.toString().replace(/\$/g,'');



if not try creating another SNC.Regex() object for the $ and use .replace()




http://wiki.servicenow.com/index.php?title=Using_Regular_Expressions_in_Scripts#gsc.tab=0


View solution in original post

3 REPLIES 3

Nate23
Mega Guru

have you tried toString() ? like:



getIdeationData.toString().replace(/\$/g,'');



if not try creating another SNC.Regex() object for the $ and use .replace()




http://wiki.servicenow.com/index.php?title=Using_Regular_Expressions_in_Scripts#gsc.tab=0


I just tested this and it worked for me:



var myReg = "hey there $ sign".replace(new RegExp("\\$","gm"),"Dollar");



so try..


getIdeationData.toString().replace(new RegExp("\\$","gm"),"");


ajitmundhekar
Tera Expert

Thanks Nate,



I tried this and it worked for me.



var reg3 = new SNC.Regex(/\$/);


getIdeationData = reg3.replaceAll(getIdeationData,'');