javascript parser for a string which contains .ini

2020-01-29 14:58发布

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?

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.)

3条回答
贼婆χ
2楼-- · 2020-01-29 15:13

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

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;
}
查看更多
姐就是有狂的资本
3楼-- · 2020-01-29 15:22

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

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

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

查看更多
老娘就宠你
4楼-- · 2020-01-29 15:29

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

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.

EDIT:

Sample of ini file read and parse

查看更多
登录 后发表回答