Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to Remove escape characters from javascript variable used inside script tag.

Ravish Rawat
ServiceNow Employee
ServiceNow Employee

i have a ui page in the html section of that ui page i am using an evaluate tag

<g:evaluate var="jvar_doc_content" object="true" expression="
var doc_content = JSON.stringify(new DocXDataProvider('08a81416db43134077d256d4ce9619b1').getDocumentJSON());
gs.info(doc_content);
doc_content;"

Now the variable - jvar_doc_content contains json value something like {"date":"2018-07-04 03:24:41","name":"xyz"}

The problem is that when i am using the variable inside jelly script tag(mentioned at last) the json is coming as 

{\"date\":\"2018-07-04 03:24:41\",\"name\":\"xyz\"} instead of  {"date":"2018-07-04 03:24:41","name":"xyz"}

Script tag

 

<script>
doc.setData(${jvar_doc_content});

</script>

10 REPLIES 10

Harsh Vardhan
Giga Patron

have you tried with this  replace(/[^\w\s]/gi, '');

 

give a try

you mean like this doc.setData((${jvar_doc_content}).replace(/[^\w\s]/gi, ''));

can you store the json result in one hidden field and then use replace() and pass the output in jvar variable .

or other way is 

or this should also work.

JSON.stringify({\"date\":\"2018-07-04 03:24:41\",\"name\":\"xyz\"} , null, '\t');

replace(/[^\w\s]/gi, ''); this does work but hides all other correctors like : , etc