all groups > dotnet jscript > october 2006 >
You're in the

dotnet jscript

group:

Replacing JS special characters with .NET


Replacing JS special characters with .NET John Mick
10/29/2006 12:00:00 AM
dotnet jscript:
Hello,

I am looking for a function which can replace all of the javascript special
characters and place \ before them.

For example I have got a string:
str = "Hi Paul How's life"; //(dotNET Server side)

should be

str = "Hi Paul How\'s life"; //(for client side Java Script)

Q1. Is there some method already present in .NET framework
Q2. Is there a list of all special characters which must be started with
backslash.

PS: I am sending ajax's xml to client.

Regards
J

Re: Replacing JS special characters with .NET Randy Webb
10/29/2006 12:00:00 AM
John Mick said the following on 10/29/2006 4:17 AM:
[quoted text, click to view]

Actually, it doesn't. The above line of code is perfectly valid client
side Javascript. The rule is that any single quote inside single quotes
should be escaped, and any double quotes inside doubles quotes should be
escaped.

[quoted text, click to view]

<shrug> Don't know, but it would be easy to do with a Regular
Expression. Simply replace " with \" in your strings, and then replace '
with \' in your strings.

str = "Some text with \"it's\" apostrophes"
pattern1 = /'/g
pattern2 = /"/g
alert(str.replace(pattern1,"\\\'").replace(pattern2,"\\\""))

That is client side code, not sure what the .NET code would be, I don't
use .NET

[quoted text, click to view]

"special characters"?

Whether it starts with a backslash or not depends on it's use. As I
said, the string "Hi Paul How's life" is perfectly acceptable in client
side Javascript, although 'Hi Paul How's life' is an error.

\n => new line
\t => tab character

That is in JS generated code, alerts, prompts, and form fields. There is
also the ETAGO issue where you should escape the / in any closing tags
in client side code.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Re: Replacing JS special characters with .NET John Mick
10/29/2006 4:50:59 PM

[quoted text, click to view]

Thank you very much for your prompt reply.

Re: Replacing JS special characters with .NET Trevor L.
10/30/2006 12:00:00 AM
[quoted text, click to view]

I am not the OP, but this line is interseting. I was stumped for a long time
when I wrote some code including tags closed with />

What exactly is "the ETAGO issue"
Is there a good reference to it?
Google found a reference on w3org but it made no sense to me.
--
Cheers,
Trevor L.
[ Microsoft MVP - FrontPage ]
MVPS Website: http://trevorl.mvps.org/
Re: Replacing JS special characters with .NET Randy Webb
10/30/2006 12:00:00 AM
Trevor L. said the following on 10/29/2006 7:05 PM:
[quoted text, click to view]

ETAGO, to the best of my knowledge, is End Tag Open shorthand. By the
specs, the browser is allowed to close a script block as soon as it
encounters the sequence </ . It doesn't even have to be the closing tag
for a script block. It can end the script block. If you have a string
that contains a closing element the the HTML parser can, at it's
discretion, close the script. There aren't any that I know of but it's
possible (and the browser would be correct by the specs) that one could
be written. So, I always escape the / to break up the sequence of </ and
it becomes <\/ which the HTML parser isn't looking for.

[quoted text, click to view]

This thread in comp.lang.javascript:

<URL:http://groups-beta.google.com/group/comp.lang.javascript/browse_thread/thread/7f61b65d36d76b13/805c86c28c71827d?lnk=gst&q=ETAGO+Richard+Cornford&rnum=1#>

Is about a good a reference as I can find right now. If you search the
clj archives for ETAGO, you can start reading on it and somewhere in
there it refers to the specs. Basically it is just the </script> that
causes problems in practice but in theory any </ can cause problems.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
AddThis Social Bookmark Button