So I am developing a form using EJS and Node.js. I pick up a text field from the database which has the following string for example:
,11-111-1111 ,Nurse ,Mobile Phone , , , , ,ABCD - LAB - User unable to see patient name in pending inquiry in Pat
Taha St reporting, she can see everything else but patient name in pending inquiry
In the Javascript code, I read this field from the database into a variable as follows:
var srDescription = '<%= Description %>';
But my Javascript breaks with the following stack in Chrome Developer tools:
VM5484:68 Uncaught SyntaxError: Unexpected token ILLEGALx.extend.globalEval @ jquery-2.0.2.min.js:4x.fn.extend.domManip
@ jquery-2.0.2.min.js:5x.fn.extend.append @ jquery-2.0.2.min.js:5(anonymous function)
@ jquery-2.0.2.min.js:5x.extend.access @ jquery-2.0.2.min.js:4x.fn.extend.html
@ jquery-2.0.2.min.js:5$.ajax.success
@ app.js:1863l @ jquery-2.0.2.min.js:4c.fireWith @ jquery-2.0.2.min.js:4k
@ jquery-2.0.2.min.js:6(anonymous function) @ jquery-2.0.2.min.js:6
Investigation:
I did some testing with the raw string & found out that this breaks only when there is a Carriage return Line feed character (CRLF)
I tried the following with no luck:
'<%= Description %>'.replace(/\r?\n\s*/g,'');
How do I escape these characters so that Javascript does not break?
You can use a plugin like ejs-shrink to clean up the whitespaces, or implement your own solution. In this case however, I think the solution would be sanitizing your data on the server side.