views:

26

answers:

1

Hi all,

Being a newbie in javascript I came to a situation where I need more information on escaping characters in a string.

Basically I know that in order to escape " I need to replace it with \" but what I don't know is for which characters I need to escape a particular string for. Is there a list of these "characters to escape"? or is it any character that is not a-zA-Z0-9 ?

In my situation, I don't have control over the content that is being displayed on my page. Users enter some text and save it. I then use a webservice to extract them from the database, build a json array of objects, then iterate the array when I need to display them. In this case, I have - naturally - no idea of what the text the user has entered and therefore for what characters I need to escape. I also use jQuery for this specific project (just in case it has a function I am not aware of, to do what I need)

Providing examples would be appreciated but I also want to learn the theory and logic behind it.

Hope someone can be of any help.

+2  A: 

There's no need to escape everything that's not a-zA-Z0-9, take a look at this example:

http://www.c-point.com/javascript_tutorial/special_characters.htm

You may also want to check out this site which holds information about escaping string, especially URLs, etc. etc.

http://www.the-art-of-web.com/javascript/escape/

Select0r