2

I would like to allow users to specify JavaScript object in a HTML textarea which I would then parse. But JSON looks too strict for me, requiring only " as quotes, and quotes everywhere. I would like that you could specify the object in ways you can specify it in JavaScript, and then parse it. How could I parse such string (in JavaScript itself)? Is there a library for it?

I know that I could use eval, but I would not like to allow custom code to run, just static object definition.

I am trying to provide a simple way for users to write MongoDB query.

Mitar
  • 6,756
  • 5
  • 54
  • 86
  • You're looking for a "JavaScript language parser written in JavaScript". Consider including "parser generator" in your search terms. See also http://stackoverflow.com/questions/2554519/javascript-parser-in-javascript – maerics Dec 12 '14 at 23:42
  • `eval` is a simple way. Depending on how exactly the input is used, it won't even be bad (from a security standpoint). Alternatives: Use a JS parser, walk the AST and extract the information you need. Or create your own parser that is a bit more relaxed than JSON (e.g. via http://pegjs.majda.cz/ or http://zaach.github.io/jison/). – Felix Kling Dec 12 '14 at 23:44
  • OK, those answers in the other question provide just answers how to parse whole JavaScript. I could now spend some time designing a grammar or making sure that only objects are read in the AST tree and then convert them manually to normal objects. But my question is: is there something which is already made for parsing only JavaScript objects. – Mitar Dec 13 '14 at 05:28

0 Answers0