tags:

views:

77

answers:

3

Any sample code how to make hashmap or similar list from

key=value CRLF   
key2=value2 CRLF
A: 

http://krook.org/jsdom/ see the String.split method. split first on crlf (\r\n I believe) and then split on "="

andersonbd1
+4  A: 

Assuming that CR, LF, and = are all protected characters (i.e., they cannot occur in key or value), you could 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];
}
VoteyDisciple
This will add a bogus key ''->undefined, due to the trailing CRLF. Best put a check that lines[i] is not blank (or maybe that it contains '=') before splitting and adding.
bobince
A: 

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];
}
kangax