PDA

View Full Version : Special Character Nightmare!


Jason_Beaudoin
04-25-2003, 07:47 PM
Hello everyone!

I'm attempting to pass a URL string from Javascript to an ASP page, and because my string contains special characters (like é, è, à, ') I'm having to use the "escape" function to encode the characters so that they are passed correctly in Netscape.

The only proble is that my string also contains apostrophes, and, as we all know well, JavaScript is delimited with a single or double apostrophes. So, when I attempt to use the escape function with my string that contains apostrophes stored in a variable, it gets cut off.

The sticker here is that I'm getting my string from a variable that is coming from an ASP form, and it reads as follows:

Véhicule blindés, Véhicule d’infanterie, Véhicule d’artillerie, Véhicule de défense antiaérienne

Therefore, I can't elimiate my apostrophes.

I've tried replacing the apostrophes with the two digit (%27) or four digit (%uxxxx) hexadecimal equivalent, but I can't get it to work.

This is what I've got:

var selectedOption = new String(f.options[f.selectedIndex].value);
var rReg = new RegExp("\\'","g");
var results = selectedOption.replace(rReg, "%27");
document.write(results);

Can anyone help me pass a string containing special characters to an ASP page? In IE, I can send the string directly to the ASP page without any problems, but in Netscape, it translates the special characters in a wierd way and returns the wrong characters.


This is the result that I get from Netscape:

/scripts/portweb.dll?query&field=keywords&op=contains&value=Véhicule blindés

But it looks normal in IE
/scripts/portweb.dll?query&field=keywords&op=contains&value=Véhicule blindés

beetle
04-25-2003, 08:15 PM
delmiting issues are only issues when you are actually using a delimeter (string literal) -- which here, you are not.

Just write the string as it comes from the option values

Jason_Beaudoin
04-25-2003, 08:20 PM
Beetle... you must have missed the last couple of lines from my post.

I can't send the string just as they are in the options values because Netscape messes up the special characters. I gave an example of the output that Netscape displays.

Does anyone have a practical solution to this? Can anyone give me an example of a working replace function in JavaScript that can replace apostrophies?

Help! I'm desperate!

Thanks! :thumbsup:

beetle
04-25-2003, 08:36 PM
hmmm, I see. Sorry, I must have read too quickly.

Erm, whave version of netscape are we talkin about here? I mean, apostrophes don't even need url-escaping.

Jason_Beaudoin
04-25-2003, 08:37 PM
Here is a great example of what I'm talking about.

<script language="JavaScript">
var newString = new String("Véhicule blindés, Véhicule d’infanterie, Véhicule d’artillerie, Véhicule de défense antiaérienne, Véhicule du génie, Véhicule de soutien, Véhicule de la Réserve, Véhicule étrangers, Aéronef, Prototype, Leopard, Coyote, VBL III");
document.write(escape(newString));

</script>

Try this in your own HTML page, and see what happens. You'll see what I mean. It gets cut off!

Jason_Beaudoin
04-25-2003, 08:40 PM
I'm using Netscape version 7.02. It makes a mess of the characters. When I encode it properly, however, it works very well, and returns the special characters very well. So, if I can send the string encoded, it would be great... but now I can't get the escape function to work right. It's really strange because I can get it to work if I write this:

<script language="JavaScript">
var newString = new String("L'ttle miss Ridding Hood");
document.write(escape(newString));

</script>

It will replace the apostrophe with "%27", but if I do it with a long string, like in my example above, it messes it up and cuts it off! Very wierd!

beetle
04-25-2003, 08:46 PM
Ooh, I see the problem. You're text has a unicode apostrophe, not an ASCII apostrophe

newString = newString.replace( /\u2019/g, "'" );

Jason_Beaudoin
04-25-2003, 08:54 PM
Thanks Beetle. I'll give this a shot and let you all know if it worked on Monday morning.

beetle
04-25-2003, 09:03 PM
I just tested it in Mozilla (same as NS7) and it worked.

Your apostrophe character was never replaced because

’ (unicode 2019)
and
' (ASCII-hex 27 ASCII-dec 39)

Are different, therefore the pattern never mathced it for replacing

Jason_Beaudoin
04-28-2003, 01:11 PM
Beattle. Thank you! Thank you so much! This works perfectly.

NinjaTurtle
04-18-2006, 09:21 AM
--deleted--

Kor
04-18-2006, 09:56 AM
Use arrays, regExp() constructor and isolate the special characters which might bring javascript errors (paranthesis for instance) in square brackets, so that the regExp will take them as literal, for instamce [(] [)]. Do a loop:
Example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var myC=[
['é','\u00e9'],
['è','\u00e8'],
['’','\u2019'],
['[(]','\u0028'],//note that the paranthesis were "isolated" into [] to match the regExp
['[)]','\u0029']
//continue with all the specila characters you need to replace
]
var myS='Véhicule blindés, Véhicule d’infanterie, (Véhicule d’artillerie), Véhicule de défense antiaérienne, Véhicule du génie, Véhicule de soutien, Véhicule de la Réserve, Véhicule étrangers, Aéronef, Prototype, Leopard, Coyote, VBL III';
for(var i=0;i<myC.length;i++){
myS=myS.replace(new RegExp(myC[i][0],'g'),myC[i][1])
}
onload=function(){
document.getElementById('myDiv').firstChild.data=myS
}

</script>
</head>
<body>
<div id="myDiv">&nbsp;</div>
</body>
</html>