3

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.

4
  • If you use a decent JSON serializer, you shouldn't have to be worried about escaping anything, it's his job :)... Commented May 21, 2010 at 8:02
  • @CMS - if I use $.getJSON of jQuery, does it handle it? or is there another way? Commented May 21, 2010 at 8:11
  • I'm talking about the JSON serializer that produces the response that you handle on the client-side. Commented May 21, 2010 at 8:19
  • @CMS - sorry if I make no sense but as I said, I am newbie and it would be great if you could show me the way :) Commented May 21, 2010 at 8:35

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.