jquery - How Do I Parse a Pipe-Delimited String into Key-Value Pairs in Javascript -


i want parse following sort of string key-value pairs in javascript object:

var stringvar = 'plnc||0|eor|<br>subd|pines|1|eor|<br>city|fort myers|1|eor|<br>'; 

each word of 4 capital letters (plnc, subd, , city) key, while word(s) in following pipe value (the first one, plnc, undefined, 1 subd 'pines', 1 city 'fort myers').

note '|eor|
' precedes every key-value pair.

what best way of doing this?

i realised it's technically csv format interesting line endings. there limitations in variable values cannot contain | or < br> since tokens define structure of string. of course escape them.

var stringvar = 'plnc||0|eor|<br>subd|pines|1|eor|<br>city|fort myers|1|eor|<br>';  function decodestring(str, variable_sep, line_endings) {     var result = [];     var lines = str.split(line_endings);     (var i=0; i<lines.length; i++) {         var line = lines[i];         var variables = line.split(variable_sep);         if (variables.length > 1) {             result[variables[0]] = variables[1];         }     }     return result; }  var result = decodestring(stringvar, "|", "<br>");  console.log(result); 

Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -