javascript parser for a string which contains .ini data

0 votes
asked Oct 6, 2010 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?

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 Answers

0 votes
answered Oct 17, 2012 by cuixiping

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

0 votes
answered Oct 9, 2013 by inquisito

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(line.length == 0 && section){
        section = null;
    };

}

return value;
}
0 votes
answered Oct 22, 2016 by erxin

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

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...