Good parser for key = value CRLF file

Any code example how to make a hashmap or similar list from

key=value CRLF   
key2=value2 CRLF

      

+2


source to share


3 answers


Assuming that CR

, LF

and =

are all protected characters (i.e. they cannot appear in key

or value

), you can simply do:



var str = "key=value\r\nkey2=value2\r\n";
var lines = str.split("\r\n");
var map = {};
for(var i = 0; i < lines.length; i++) {
  var pieces = lines[i].split("=");
  if (pieces.length == 2)
    map[pieces[0]] = pieces[1];
}

      

+6


source


This should do it:



var str = "key=value\r\nkey2=value2\r\n";

var re = /([^=]*)=(.*?)\r\n/g, match, map = { };

while (match = re.exec(str)) {
  map[match[1]] = match[2];
}

      

+1


source


http://krook.org/jsdom/ see String.split method. first split into crlf (I suppose) and then split into "="

0


source







All Articles