How do I escape backslashes in JSON?

Posted by peteb on Stack Overflow See other posts from Stack Overflow or by peteb
Published on 2010-06-13T23:57:21Z Indexed on 2010/06/14 0:02 UTC
Read the original article Hit count: 190

Filed under:
|
|

I am using Firefox's native JSON.parse() to parse some JSON strings that include regular expressions as values, for example:

var test = JSON.parse('{"regex":"/\\d+/"}');

The '\d' in the above throws an exception with JSON.parse(), but works fine when I use eval (which is what I'm trying to avoid).

What I want is to preserve the '\' in the regex - is there some other JSON-friendly way to escape it?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about JSON