Solved

# Escape in eval function on Single Quotation

Posted on 2006-07-07
Medium Priority
714 Views
Try these two sample scripts:

This works:
<script>
var boys = 'He said \'howdy\', I said \'hi\'.' // nesting single quotes within single quotes
</script>

This does not work:
<script>
var boys = eval("'He said \'howdy\', I said \'hi\'.'") // nesting single quotes within single quotes
</script>

Why is this? Does not eval handle escape singel character?
And what should I put instead of \' in the eval function to make it work?

Thank you,
Best regards,
Marius
0
Question by:traelnes
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 2

Expert Comment

ID: 17057510
Hi Marius,

try this
<script>
var val='He said \'howdy\', I said \'hi\'.';
var boys = eval("val"); // nesting single quotes within single quotes
</script>

regards,
vikrant
0

LVL 22

Expert Comment

ID: 17057519
Hello traelnes,

because \' itself is eval again and after eval("'") you've get single quote that have to be evaled again and you get unterminated string constant.

Why you have to eval this? just concatenate...

HTH

I
0

LVL 63

Accepted Solution

Zvonko earned 2000 total points
ID: 17057693
Escape chars has to be escaped in eval():

var boys = eval("'He said \\'howdy\\', I said \\'hi\\'.'");

0

Author Comment

ID: 17057957
Thank you Zvonko for this. It works.

Just curious about what ivostoykov mean by concatenate?
0

LVL 63

Expert Comment

ID: 17058099
You are welcome.
Let us wait and see what concatenate trick comes along ;-)
0

Author Comment

ID: 17066600
No Concentate. Ok, Zvonko, you are the winner!
0

LVL 63

Expert Comment

ID: 17067556
:)
Thanks.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
###### Suggested Courses
Course of the Month10 days, left to enroll