SyntaxError: unterminated string literal

615 views
Skip to first unread message

Vladimir Tkach

unread,
May 21, 2009, 10:24:54 AM5/21/09
to prototype-s...@googlegroups.com
Prototype gsub error?

$H({s:'\'}).toJSON()

Result :
SyntaxError: unterminated string literal

--
Best Regards,
Vladimir Tkach

+972-54-7574166
tea...@gmail.com

http://teamco-anthill.blogspot.com/

Chris Sansom

unread,
May 21, 2009, 10:32:44 AM5/21/09
to prototype-s...@googlegroups.com
At 17:24 +0300 21/5/09, Vladimir Tkach wrote:
>Prototype gsub error?
>
>$H({s:'\'}).toJSON()
>
>Result : SyntaxError: unterminated string literal

I /think/, though the experts hereabouts will confirm or otherwise,
that JavaScript thinks the \ is escaping the quote that comes after
it. To do what you want, I believe you'd have to escape the backslash
in turn:

$H({s:'\\'}).toJSON()

I hope that's right!

--
Cheers... Chris
Highway 57 Web Development -- http://highway57.co.uk/

Why do you necessarily have to be wrong just because
a few million people think you are?
-- Frank Zappa

Richard Quadling

unread,
May 21, 2009, 11:07:03 AM5/21/09
to prototype-s...@googlegroups.com
2009/5/21 Chris Sansom <ch...@highway57.co.uk>:
>
> At 17:24 +0300 21/5/09, Vladimir Tkach wrote:
>>Prototype gsub error?
>>
>>$H({s:'\'}).toJSON()
>>
>>Result : SyntaxError: unterminated string literal
>
> I /think/, though the experts hereabouts will confirm or otherwise,
> that JavaScript thinks the \ is escaping the quote that comes after
> it. To do what you want, I believe you'd have to escape the backslash
> in turn:
>
> $H({s:'\\'}).toJSON()
>
> I hope that's right!
>
> --
> Cheers... Chris
> Highway 57 Web Development -- http://highway57.co.uk/
>
> Why do you necessarily have to be wrong just because
> a few million people think you are?
>    -- Frank Zappa
>
> >
>

Yep. \ is a magic character in many languages.

\r
\n
\t

to name just 3 things (return, newline, tab).

\\

is to allow the escape character through.

Watch out when building JS code from other languages.

<?php
$s_JavaScriptString = 'document.write(\'John O\\\'Connor\');';
echo $s_JavaScriptString;
?>

for example.

Ok. Bad example, but as you start passing the text through different
engines, the \ needs to be escaped for each level.

So, PHP generating JS generating HTML content ... having anything to
escape has to be done carefully.



--
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

Vladimir Tkach

unread,
May 21, 2009, 2:07:00 PM5/21/09
to prototype-s...@googlegroups.com
OK

Thanks for your comments


2009/5/21 Richard Quadling <rqua...@googlemail.com>
Reply all
Reply to author
Forward
0 new messages