JavaScript implements the method of parsing INI file content _javascript Tips

Source: Internet
Author: User


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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.