Converting multiline, indented json to single line using javascript

前端 未结 2 1642
遇见更好的自我
遇见更好的自我 2021-01-28 03:18

I\'ve come up with the following function for converting a multiline, nicely indented json to a single line

function(text) {
    var outerRX = /((?:\".*?\")|(\\s         


        
2条回答
  •  情歌与酒
    2021-01-28 03:52

    This fixes the two bugs in the question, but probably not very efficiently

    function(text) {
        var outerRX = /((?:"([^"]|(\\"))*?(?!\\)")|(\s|\n|\r)+)/g,
        innerRX = /^(\s|\n|\r)+$/;
    
        return text.replace(outerRX, function($0, $1) {
            return $1.match(/^(\s|\n|\r)+$/) ? "" : $1 ;
        });
    }
    

提交回复
热议问题