[thelist] [JavaScript] Avoid Escaping Characters

Andrew Clover and-evolt at doxdesk.com
Mon May 24 09:32:16 CDT 2004


> Does such a feature exist in JavaScript? I am not aware of one.

If you mean something like Python's raw-strings:

   x= r'"\" is a literal blackslash.'

then no, I'm afraid not. You'll have to double-up backslash characters 
before outputting to a JS string literal.

If you are outputting arbitrary strings into a JS string literal inside 
a <script> block or inline event handler (eg. <a onclick="alert('some 
arbitrary string')">) you'll have to look out for the <, & and " 
characters as well as \, plus CR and LF (ASCII 13 and 10) which will 
confuse JavaScript, if multi-line strings are a possibility. In this 
case it is best to replace all these out-of-bounds characters with 
hexadecimal character escapes (\x0A and so on).

-- 
Andrew Clover
mailto:and at doxdesk.com
http://www.doxdesk.com/


More information about the thelist mailing list