Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  70] [ 6]  / answers: 1 / hits: 25510  / 14 Years ago, sun, june 13, 2010, 12:00:00

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?


More From » json

 Answers
199

You need to escape the escape backslashes already in there :) like this:



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


You can test it a bit here: http://jsfiddle.net/h3rzE/


[#96511] Thursday, June 10, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austenjordang

Total Points: 544
Total Questions: 112
Total Answers: 112

Location: Monaco
Member since Sun, Jan 16, 2022
2 Years ago
;