String replace all occurance of \" , "{ and "}

deepak50
Tera Contributor

Hi ,
I have to replace all occurances of  \" , "{ and "} from response of API. I am using replaceAll function of string as below but it is not working

var str2=responseBody.toString().replaceAll('\"','"');
var str3=str2.replaceAll('"{','{');
var str4=str3.replaceAll('"}','}');

This is not replacing these char , Please let me know how can we replace all chars as required.

Thanks
Deepak

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Deepak,

Can't replace backslash because it's an escape character. Backslash and the next character will be treated as 1 character in JavaScript. This is JavaScript specification.

View solution in original post

14 REPLIES 14

Yes, but the ECMAScript standard allows the use of regexp in the first argument of replaceAll. The SN implementation will accept a regexp but converts it to a string, and does not throw any errors. This leads to weird behaviours. That is a bug.

JamesFricker_0-1734301001098.png

 

JamesFricker_1-1734301809317.png

 

Plus there is an OOTB script includes that runs in the global scope and uses replaceAll 4 times with a regexp in the first argument. Which is never going to work.

JamesFricker_3-1734302555700.png

So perhaps SN should practise what they preach.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

what is not getting replaced? the \ character?

Regards
Ankur

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

i need to replace as below:

\" -->"
"{--> {
"}-->}

so what is not getting replaced?

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

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Deepak,

Can't replace backslash because it's an escape character. Backslash and the next character will be treated as 1 character in JavaScript. This is JavaScript specification.