- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 12:13 PM
If I try below code in W2Javascript emultor then it is working, however when I tried the same in ServiceNow it is giving me an error.
The line in the question is "replace(/\"/g, '"').replace(/\\/g, '')"
Code:
To try click here and replace all the HTML with below:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The replace() Method</h2>
<p>replace() searches a string for a value,
and returns a new string with the specified value(s) replaced:</p>
<script>
let str = '[{"orderCloseOut":{"workOrderId":"123","status":"{\"success\":{\"code\":\"200\",\"message\":\"Status update request processed\"}}"}},{"orderCloseOut":{"workOrderId":"456","status":"{\"success\":{\"code\":\"200\",\"message\":\"Status update request processed\"}}"}},"{\"orderCloseOut\":{\"workOrderId\":\"999\",\"status\":{\"error\":{\"code\":\"404\",\"message\":\"workOrderId: not valid or not found\"}}}}"]';
let res = str.replace(/\"/g, '"').replace(/\\/g, '');
document.getElementById("demo").innerHTML = res;
</script>
</body>
</html>
However, when I tried the same line that does the replacement then in ServiceNow I get below error during compile time:
Unnecessary Escape Character: \".
Answers/comments will be marked helpful/correct if applicable.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 12:25 PM
NM, I was able to achieve with below replace statement.
.replace("\"", '"').replace("\\","").replace("\"{","{").replace("}\"","}");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 12:25 PM
NM, I was able to achieve with below replace statement.
.replace("\"", '"').replace("\\","").replace("\"{","{").replace("}\"","}");