如何在javascript中编写解析器,用编辑器将编辑器中的文本解析成JSON? [英] How to write a parser in javascript to parse text in editor into JSON with a model?

查看:101
本文介绍了如何在javascript中编写解析器,用编辑器将编辑器中的文本解析成JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有人问这个问题。但我会尝试。

I don't know if it is the place to ask this. But I will try.

我需要的是,例如,如果用户在Code Mirror编辑器中写入一些文本,如:

What I need is, for example, if user writes some text in Code Mirror editor like:

a1=Some Text
    a11 = Some Child text

并且解析器应检查语法是否正确,并将其解析为:

and the parser should check if the grammer is correct, and parse it to:

 {
    'a1' : {
             'value' : "Some Text",
             'a11' : "some child text"
    }
 }

根据规格,这样的东西。

Something like this according to the gramme.

那么,是否有任何解析器库或javascript中的东西?

So, is there any parser library or something in javascript?

推荐答案

您应该能够使用 PEG.js 来解析客户端上的文本。我还建议从为用户期望的任何有效输入编写正确的语法开始。

You should be able to use PEG.js to parse the text on the client side. I would also recommend to start off from writing a proper grammar for whatever valid input you are expecting from the user.

这篇关于如何在javascript中编写解析器,用编辑器将编辑器中的文本解析成JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆