- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2022 03:36 AM
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
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2022 09:36 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-15-2024 02:17 PM - edited ‎12-15-2024 02:43 PM
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.
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.
So perhaps SN should practise what they preach.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2022 03:45 AM
Hi,
what is not getting replaced? the \ character?
Regards
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
‎05-25-2022 04:58 AM
i need to replace as below:
\" -->"
"{--> {
"}-->}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2022 09:30 PM
so what is not getting replaced?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2022 09:36 PM
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.