Regex to match nested json objects

前端 未结 4 1658
失恋的感觉
失恋的感觉 2021-01-02 14:05

I\'m implementing some kind of parser and I need to locate and deserialize json object embedded into other semi-structured data. I used regexp:



        
4条回答
  •  被撕碎了的回忆
    2021-01-02 14:15

    As others have suggested, a full-blown JSON parser is probably the way to go. If you want to match the key-value pairs in the simple examples that you have above, you could use:

    (?<=\{)\s*[^{]*?(?=[\},])
    

    For the input string

    {title:'Title',  {data:'Data', {foo: 'Bar'}}}
    

    This matches:

     1. title:'Title'
     2. data:'Data'
     3. foo: 'Bar'
    

提交回复
热议问题