Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

double and single quotes in a string

Status
Not open for further replies.

loosecannon1

Programmer
Feb 19, 2002
55
US
I have a CGI process that dynamically populates an html form with parsed data from other html files.

Everything works fine except when the data contains double quotes.

My O’Reilly book says, “Double-quote characters may be contained within strings delimited by single-quote characters, and single-quote characters may be contained within strings delimited by double quotes.”

My problem is that the string may contain BOTH!

I’ve even tried to regex the string for ’ and replace with \’, but I still get an error

So my question is, “How can I display a string that has both single and double quotes in it?”



<script language="javascript">
var text = "'single' and “double” quotes";
alert(text);
</script>


Thanks!
 
The \ is correct, but you need to do the search and replace in the CGI, not JavaScript. Also, make sure that the character you're escaping with the \ is the same as what your string is surrounded by. It does no good to replace single quotes with \' if your string is surrounded with double quotes.

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
I have a VBScript function that changes single quotes to a tic mark(`) and double quotes to a double tic(``) if that would help....

Let me know and I'll post it if you want it.
 
Use double-quotes to delimit your string, and replace all " characters with &quot; - which is the correct way of displaying double-quotes in HTML anyway.

Hope this helps,
Dan
 

Incidentally, if the quotes are those "fancy" double-quotes taht things like Word produce, then they do not need escaping. However, I'd repalce all of those with &quot; too, as they don't display correctly across all O/S (speaking from experience on this one ;o)

Dan
 
Another way: always escape both ' and " C-like (\' and \"). This works no matter what kind of string delimiter you use. Plus string length remains unchanged.

However, for HTML displaying purposes it is good to have centralized function that replaces HTML delimiter characters into SGML crap - &quot; for single quote, &lt; for <, &gt; for > and so on. Many server-side languages have such functions - PHP htmlentities(), ASP Server.HTMLEncode()...
 

>> &quot; for single quote

Are you sure? This would turn ' into &quot;... You only want to turn double-quotes (") into &quot;, I'd have thought.

Dan
 
thanks for the posts

I ended up escaping the string in perl(cgi) instead of javascript and it worked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top