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(/[\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;
}
try {
var data = fs.readFileSync('C:\\data\\data.dat', 'utf8');
var javascript_ini = parseINIString(data);
console.log(javascript_ini['Section1']);
}
catch(e) {
console.log(e);
}
Also in JavaScript:
- js array find regex
- convert milliseconds to time javascript
- align left text in reactstrap
- javascript flat object
- keydown shift tab angular
- recorrer letra por letra js
- how to check string uppercase or lowersace using regex javascript
- how to get window size in react js
- UTL ,Raw json
- is json valid with single quotes
- string to char array javascript
- react axios download zip file
- node-fetch auth basic
- get current week number javascript
- how to find out which version of React
- convert date to unix timestamp javascript
- call function with id javascript
- how to get user's machine and browser through js
- javascript detect click outside element
- prime numbers javascript
- extract string from text file javascript
- Sets can be used to store __________. in js
- créer composant react
- js opposite of preventdefault