Global replace not working in ServiceNow scripting

TT3
Kilo Guru

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.

1 ACCEPTED SOLUTION

TT3
Kilo Guru

NM, I was able to achieve with below replace statement.

.replace("\"", '"').replace("\\","").replace("\"{","{").replace("}\"","}");

View solution in original post

1 REPLY 1

TT3
Kilo Guru

NM, I was able to achieve with below replace statement.

.replace("\"", '"').replace("\\","").replace("\"{","{").replace("}\"","}");