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

后端 未结 6 1217
醉梦人生
醉梦人生 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:10

    Cleanest way possible, you can modify the source to split by a different delimiter.

    https://gist.github.com/allensarkisyan/5873977#file-parsequerystring-js

    `/**
    * @name - parseQueryString 
    * @author - Allen Sarkisyan
    * @license - Open Source MIT License
    *
    * @description - Parses a query string into an Object.
    * - Optionally can also parse location.search by invoking without an argument
    */`
    
    `
    function parseQueryString(queryString) {
        var obj = {};
        function sliceUp(x) { x.replace('?', '').split('&').forEach(splitUp); }
        function splitUp(x) { var str = x.split('='); obj[str[0]] = decodeURIComponent(str[1]); }
        try { (!queryString ? sliceUp(location.search) : sliceUp(queryString)); } catch(e) {}
       return obj;
    }
    `
    

提交回复
热议问题