"Randy Webb" <HikksNotAtHome@aol.com> wrote in message
news:8JadnX70BfISF9nY4p2dnA@telcove.net...
> John Mick said the following on 10/29/2006 4:17 AM:
>> 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)
>
> 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.
>
>> Q1. Is there some method already present in .NET framework
>
> <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
>
>> Q2. Is there a list of all special characters which must be started with
>> backslash.
>
> "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 > Javascript Best Practices -
>
http://www.JavascriptToolbox.com/bestpractices/ Thank you very much for your prompt reply.