Forum Moderators: open

Message Too Old, No Replies

String delimiters

A really basic question

         

amsmota

6:12 pm on Jul 28, 2005 (gmt 0)

10+ Year Member



This seems so basic that i'm ashamed of ask, but...

In JavaScript/JSP, i have code like this:

arrWorkList[<%= i %>] = {
atributoA:"<%= i + 1 %>",
atributoB:"<%= (line[1]%>" }

Since i have no control on what the JSP will put on line[1], it can have (and *does* have) words like

"Café d'Ouro"

so i get a error either i have

atributoB:"<%= (line[1]%>"
atributoB:'<%= (line[1]%>'

so what alternative have i?

Thanks a lot.

garann

8:13 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



I'm guessing you don't have control over the backend code and what line[1] initially gets populated with? If so, you can try

<%= (line[1]).replace("'","''") %>

I'm not totally sure on the syntax or whether those are the right quotes, but hopefully you get the idea.

replace method [java.sun.com]

amsmota

11:28 am on Aug 2, 2005 (gmt 0)

10+ Year Member



Thanks for the help. The replace method only accepts a char in both params, not a String, so that won't do. But since yes, i have control over the jsp, i ended up using something similar along that line, the jakarta commons StringEscapeUtils. It has escapeHtml, escapeJava and escapeJavaString that covers all my needs.

However i wonder what can i do if i wanted a client-side Javascript only solution, as that is one situation that may occur.

Thanks again.

garann

4:51 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



In my experience, you really need to escape those variables at the server. Once you write them to the page, they'll complain immediately if the quotes don't match up.

If you pass a variable to Javascript, you can do escaping very easily, so hopefully that's the situation in which you'll need it? But your Java code obviously can't pass Javascript a variable, so escaping these at the server is really the way to go.

amsmota

5:06 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



Well, i was thinking in going to the server via XmlHttpRequest (yes, i'm a ajax guy ;) )and manipulate the resulting string via Javascript, so how can i "do escaping very easily"?

I mean all the escape, the HTML escape chracters and this case of " and '.

Am i forgetting something?

garann

5:47 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



I'm afraid I don't know anything about the AJAX end of it, but the Javascript replace method is much the same as Java's. You'll want to supply it a regular expression and a replacement string. Good luck!

amsmota

10:04 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



Well, ajax is just that, javascript and xmlhttrequest. and the dom... and css... and xml... and xslt... and whatever!

When you said "escaping very easily" i thougth there was something like the jarkata commons StringEscapeUtilities, that realy does it simply...

So you say i must use replace to replace ' with '', " with \", & with &amp; and the rest? That's not "very easily", i mean, it's easy but a lot of typing...

Oh well, so be it...

Thanks a lot.