Skip to content

Instantly share code, notes, and snippets.

@jrichardsz
Created April 21, 2024 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrichardsz/16dab906bbdff6b6dae3834526c26bce to your computer and use it in GitHub Desktop.
Save jrichardsz/16dab906bbdff6b6dae3834526c26bce to your computer and use it in GitHub Desktop.
parse init file with 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment