Douglas Crockford <> writes:
....
> guarded by a single regexp test to assure that it is safe.
>
> JSON.parse = function (text) {
> return
> (/^(\s|[,:{}\[\]]|"(\\["\\bfnrtu]|[^\x00-\x1f"\\])*"|-?\d+(\.\d*)?([eE][+-]?\d+)?|true|false|null)+$/.test(text))
Looks reasonable (but a comment stating what it is supposed to match
would would make it much more readable
For efficiency, I'd change \s to \s+.
If the regexp doesn't match, then false is returned. This can also
be the value of the JSON expression. Perhaps it would be safer to
return undefined if the test fails, i.e.,
re.test(text) ? eval("("+test+")") : undefined;
or
if(re.test(text)) { return eval("("+test+")"); }
Also, you could move the creation of the RegExp object out of the
function, and reuse it for each call, instead of creating a new,
lengthy, RegExp for each call. However, that is only important if
calls are frequent, which they probably shouldn't be anyway.
/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'