Split a pipe delimited key-value pair separated by '=' symbol

后端 未结 6 1237
醉梦人生
醉梦人生 2020-12-29 08:46

We are receiving an input parameter value as a pipe-delimited key-value pair, separated with = symbols. For example:

\"|User=0101|Name=ImNewUse         


        
6条回答
  •  情深已故
    2020-12-29 09:13

    I would just use regular expressions to group (see here) each KEY=VALUE pair and then iterate over them to fill up the JSON object. So you could have something like this:

    var re = /(([^=\|]+)=([^=\|]+))/g;
    var match;
    var myString = "|User=0101|Name=ImNewUser|IsAdmin=0|RefId=23ae2123cd223bf235|";
    while (match = re.exec(myString)) {
        console.log(match);
        // first iteration returns ["User=0101","User=0101","User","0101"] and so on
    }
    

提交回复
热议问题