.ini 是Initialization File的缩写,即初始化文件,ini文件格式广泛用于软件的配置文件。
INI文件由节、键、值、注释组成。
根据node.js版本的node-iniparser改写了个JavaScript函数来解析INI文件内容,传入INI格式的字符串,返回一个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(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; }
测试INI内容:
返回结果对象:
Copyright © 2019- howto1234.net 版权所有 湘ICP备2023021910号-3
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务