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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3870019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:30:43  来源:igfitidea点击:

javascript parser for a string which contains .ini data

javascriptparsing

提问by sat

If a string contains a .ini file data , How can I parse it in JavaScript ? Is there any JavaScript parser which will help in this regard?

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

here , typically string contains the content after reading a configuration file. (reading cannot be done through javascript , but somehow I gather .ini info in a string.)

在这里,通常 string 包含读取配置文件后的内容。(阅读无法通过 javascript 完成,但我以某种方式将 .ini 信息收集在一个字符串中。)

回答by cuixiping

I wrote a javascript function inspirated by node-iniparser.js

我写了一个受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;
}

2017-05-10 updated: fix bug of keys contains spaces.

2017-05-10 更新:修复键包含空格的问题。

EDIT:

编辑:

Sample of ini file read and parse

ini文件读取和解析示例

回答by Inquisito

Here's a function who's able to parse ini data from a string to an object! (on client side)

这是一个能够将 ini 数据从字符串解析为对象的函数!(在客户端)

function parseINIString(data){
var regex = {
    section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
    param: /^\s*([\w\.\-\_]+)\s*=\s*(.*?)\s*$/,
    comment: /^\s*;.*$/
};
var value = {};
var lines = data.split(/\r\n|\r|\n/);
var section = null;

for(x=0;x<lines.length;x++)
{

    if(regex.comment.test(lines[x])){
        return;
    }else if(regex.param.test(lines[x])){
        var match = lines[x].match(regex.param);
        if(section){
            value[section][match[1]] = match[2];
        }else{
            value[match[1]] = match[2];
        }
    }else if(regex.section.test(lines[x])){
        var match = lines[x].match(regex.section);
        value[match[1]] = {};
        section = match[1];
    }else if(lines.length == 0 && section){//changed line to lines to fix bug.
        section = null;
    };

}

return value;
}

回答by Erxin

You could try the config-ini-parser, it's similar to python ConfigParserwithout I/O operations

你可以试试config-ini-parser,它类似于没有 I/O 操作的python ConfigParser

It could be installed by npm or bower. Here is an example:

它可以通过 npm 或 bower 安装。下面是一个例子:

var ConfigIniParser = require("config-ini-parser").ConfigIniParser;
var delimiter = "\r\n"; //or "\n" for *nux

parser = new ConfigIniParser(delimiter); //If don't assign the parameter delimiter then the default value \n will be used
parser.parse(iniContent);
var value = parser.get("section", "option");
parser.stringify('\n'); //get all the ini file content as a string

For more detail you could check the project main page or from the npm package page

有关更多详细信息,您可以查看项目主页或npm 包页面