Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js read a ini file

/* 

Read a ini file

[Section1]
Param1=value1
[Section2]
Param2=value2

*/
var fs = require("fs");

function parseINIString(data){
    var regex = {
        section: /^s*[s*([^]]*)s*]s*$/,
        param: /^s*([^=]+?)s*=s*(.*?)s*$/,
        comment: /^s*;.*$/
    };
    var value = {};
    var lines = data.split(/[
]+/);
    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;
}

try {
    var data = fs.readFileSync('C:datadata.dat', 'utf8');

    var javascript_ini = parseINIString(data);
    console.log(javascript_ini['Section1']);

} 
catch(e) {
    console.log(e);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: node terminal readline console 
Javascript :: sveltekit tailwind 
Javascript :: react alice carousel 
Javascript :: standalone apk build expo 
Javascript :: js get index from foreach 
Javascript :: copy element jquery 
Javascript :: disable input box javascript 
Javascript :: remove all sign that is not a-b in string js 
Javascript :: How to create sequelize connection in javascript 
Javascript :: how to create json file in c# 
Javascript :: Object of type * is not JSON serializable 
Javascript :: console log error javascript 
Javascript :: falsy values in javascript 
Javascript :: javascript check undefined 
Javascript :: closures in javascript 
Javascript :: element remove class 
Javascript :: average of numbers 
Javascript :: js how to convert vh to pixel 
Javascript :: discord.js clear console 
Javascript :: darkmode js 
Javascript :: blob to text javascript 
Javascript :: printing in a single line in javascript 
Javascript :: price range slider bootstrap 4 
Javascript :: javascript download html to pdf 
Javascript :: number to float js 
Javascript :: encrypt in js 
Javascript :: javascript button click event 
Javascript :: mongodb replace string 
Javascript :: convert string to camelcase 
Javascript :: remove eslint check react native 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =