包含.ini数据的字符串的javascript解析器

sat*_*sat 10 javascript parsing

如果字符串包含.ini文件数据,我如何在JavaScript中解析它?是否有任何JavaScript解析器在这方面有所帮助?

这里,通常字符串包含读取配置文件后的内容.(读取不能通过javascript完成,但不知何故,我在字符串中收集.ini信息.)

cui*_*ing 19

我写了一个由node-iniparser.js启发的javascript函数

function parseINIString(data){
    var regex = {
        section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
        param: /^\s*([^=]+?)\s*=\s*(.*?)\s*$/,
        comment: /^\s*;.*$/
    };
    var value = {};
    var lines = data.split(/[\r\n]+/);
    var section = null;
    lines.forEach(function(line){
        if(regex.comment.test(line)){
            return;
        }else if(regex.param.test(line)){
            var match = line.match(regex.param);
            if(section){
                value[section][match[1]] = match[2];
            }else{
                value[match[1]] = match[2];
            }
        }else if(regex.section.test(line)){
            var match = line.match(regex.section);
            value[match[1]] = {};
            section = match[1];
        }else if(line.length == 0 && section){
            section = null;
        };
    });
    return value;
}
Run Code Online (Sandbox Code Playgroud)

2017-05-10更新:修复密钥错误包含空格.

编辑:

ini文件的样本读取和解析