正则表达式,使用嵌套的{{{...},{...}},},{...}在每个父项{}上拆分字符串

时间:2019-06-10 20:55:35

标签: javascript regex

我在一个开源项目中工作。它基本上从查询字符串生成json响应。

错误。

我正在测试该项目,但发现了正则表达式中的错误(如果我不是上帝:()

这是我正在使用的正则表达式。

            (?:(\{).*?(\})|[^,])+

基本上,当匹配模式时,它会分割字符串 例子。

        // string example
        a:{anyhing},b:{anything},c:{anything}

        // expected
        [
            "a:{anyhing}",
            "b:{anything}",
            "c:{anything}"
        ]

案例1.成功 我要传递的字符串

            "a:{m:st,n:{x:st}},b:{m:st}"

预期结果:成功

            [
                "a:{m:st,n:{x:st}}",
                "b:{m:st}"
            ]

情况2。失败 我要传递的字符串

            "a:{n:{x:st},m:st},b:{m:st}"

意外结果

            [
                "a:{n:{x:st}",
                "m:st}",
                "b:{m:st}",
            ]               

预期结果:应该类似于案例1

            [
                "a:{n:{x:st},m:st}",
                "b:{m:st}"
            ]

谢谢。

请在此处随意阅读有关该项目的信息 https://github.com/rollrodrig/customjsonbuilder 和这里 https://www.npmjs.com/package/customjsonbuilder

编辑:

该字符串可能像这样嵌套{}

            // string
            "a:{m: {x:str,y:str}, n:str },b:{m:str,n:str},c:{t:string}"
            // expected
            [
                "a:{m: {x:str,y:str}, n:str }",
                "b:{m:str,n:str}",
                "c:{t:string}",
            ]

            // string
            "a:{m: {x:{d:str,i:str},y:str}, n:str },b:{m:str,n:str},c:{t:string}"
            // expected
            [
                "a:{m: {x:{d:str,i:str},y:str}, n:str }",
                "b:{m:str,n:str}",
                "c:{t:string}"
            ]

            // string
            "a:{n:{x:{h:{i:string},x:{d:string}}},m:{h:string}},b:{k:string,l:{u:str,r:str}},c:{t:string}"
            // expected
            [
                "a:{n:{x:{h:{i:string},x:{d:string}}},m:{h:string}}",
                "b:{k:string,l:{u:str,r:str}}",
                "c:{t:string}"
            ]

1 个答案:

答案 0 :(得分:0)

好吧,经过一番思考,我决定更改代码。这次我决定不使用正则表达式。

            export const comaDivider = (string:string) => {
                let braces:number = 0;
                let indexes:number[] = [];
                let divided:string[] = [];
                let l = string.length;
                for (let i = 0; i < l; i++) {
                    let c = string.charAt(i);
                    if(c === "{")
                        braces++;
                    if(c === "}")
                        braces--;
                    if(c === "," && braces === 0) 
                        indexes.push(i);
                }
                indexes.push(string.length);
                indexes.reduce((prev:number,next:number)=>{
                    divided.push(string.substring(prev,next));
                    return next+1;
                },0)
                return divided;
            }

单元测试成功通过

            it('run', () => {
                expect(comaDivider("name:string,email:string"))
                    .to.deep.equal(["name:string","email:string"]);
                expect(comaDivider("name:string,nicks:{first:string,last:string}"))
                    .to.deep.equal(["name:string","nicks:{first:string,last:string}"]);
                expect(comaDivider("name:string,email:string,nicks:{first:string,last:string}"))
                    .to.deep.equal(["name:string","email:string","nicks:{first:string,last:string}"]);
                expect(comaDivider("name:string,nicks:{first:string,last:string},email:string"))
                    .to.deep.equal(["name:string","nicks:{first:string,last:string}","email:string"]);
                expect(comaDivider("name:string,nicks:{first:string,last:{year:number,age:23}},email:string"))
                    .to.deep.equal(["name:string","nicks:{first:string,last:{year:number,age:23}}","email:string"]);
                expect(comaDivider("name:string,nicks:[{first:string,last:string}]"))
                    .to.deep.equal(["name:string","nicks:[{first:string,last:string}]"]);
                expect(comaDivider("name:string,nicks:[{first:string,last:string};10]"))
                    .to.deep.equal(["name:string","nicks:[{first:string,last:string};10]"]);
                expect(comaDivider("name:string,nicks:[{first:string,last:[{name:string, age:23};5]};10],other:string"))
                    .to.deep.equal(["name:string","nicks:[{first:string,last:[{name:string, age:23};5]};10]","other:string"]);
                expect(comaDivider("name:string,nicks:{first:string,last:[{name:string, age:23};5]},other:string"))
                    .to.deep.equal(["name:string","nicks:{first:string,last:[{name:string, age:23};5]}","other:string"]);
            });

            it('fixing bug', () => {
                let r1 = [];
                r1 = comaDivider("a:string,b:string");
                expect(r1).to.deep.equal(["a:string","b:string"])
                r1 = comaDivider("a:string,b:string,c:string");
                expect(r1).to.deep.equal(["a:string","b:string","c:string"])
                r1 = comaDivider("a:{m:{x:string},n:strin},b:string");
                expect(r1).to.deep.equal(["a:{m:{x:string},n:strin}","b:string"])
                r1 = comaDivider("a:{n:strin,m:{x:string}},b:string");
                expect(r1).to.deep.equal(["a:{n:strin,m:{x:string}}","b:string"])
                r1 = comaDivider("b:{x:{n:string},y:{m:string}},a:string");
                expect(r1).to.deep.equal(["b:{x:{n:string},y:{m:string}}","a:string"])
                r1 = comaDivider("b:{x:{n:string},y:{m:string,h:{t:string,r:string}}},a:string");
                expect(r1).to.deep.equal(["b:{x:{n:string},y:{m:string,h:{t:string,r:string}}}","a:string"])
                r1 = comaDivider("b:{x:{n:string}},y:{m:string},a:string");
                expect(r1).to.deep.equal(["b:{x:{n:string}}","y:{m:string}","a:string"])
                r1 = comaDivider("b:{x:{n:string},y:{m:string,h:{t:string,r:string}}},a:string");
                expect(r1).to.deep.equal(["b:{x:{n:string},y:{m:string,h:{t:string,r:string}}}","a:string"])
                r1 = comaDivider("y:string,b:{x:{n:string},y:string},a:{s:{d:string}}");
                expect(r1).to.deep.equal(["y:string","b:{x:{n:string},y:string}","a:{s:{d:string}}"])
                r1 = comaDivider("b:string,c:{m:string,n:string},d:string");
                expect(r1).to.deep.equal(["b:string","c:{m:string,n:string}","d:string"])
                r1 = comaDivider("b:{x:string,y:{y:string,w:num}},c:{m:string,n:string},d:string,f:{h:num,r:str},to:{na:str}");
                expect(r1).to.deep.equal(["b:{x:string,y:{y:string,w:num}}","c:{m:string,n:string}","d:string","f:{h:num,r:str}","to:{na:str}"])
            })

如果有人对此代码有改进,请告诉我。