This article is an example of how JavaScript implements parsing INI file content. Share to everyone for your reference, specific as follows:
. INI is the abbreviation for initialization file, which is the initialization files, and the INI file format is widely used in software configuration files.
The INI file consists of sections, keys, values, and comments.
According to the node.js version of the Node-iniparser rewrite a JavaScript function to parse the INI file content, passed in the INI format string, return a JSON object.
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;
Lines.foreach (function (line) {
if (regex.comment.test) {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;
}
Test INI content:
Returns the result object:
More readers interested in JavaScript-related content can view the site topics: "JavaScript traversal algorithm and tips summary", "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills summary", " JavaScript animation effects and tips Summary, "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary" and "JavaScript Mathematical Computing Usage Summary"
I hope this article will help you with JavaScript programming.