Regex to find if there is only one block of code

前端 未结 4 1498
滥情空心
滥情空心 2021-01-07 12:51

My input is a string, I want to validate that there is only one first-level block of code.

Examples :

{ abc }              TRUE
{ a { bc }         


        
4条回答
  •  隐瞒了意图╮
    2021-01-07 13:19

    This is a terrible workaround.

    Since this is in Javascript there's not really much to do, but please see the following regex:

    /^{([^{}]*|{})*}$/
    

    Where you copy ([^{}]*|{})* and insert it between the last pair of curly brackets (rinse and repeat). Every duplication of this pattern allows another level of nesting between your elements. (This is a workaround for the lack of recursion in JS regex, required to solve nesting problems.)

    Online Regex Demo

提交回复
热议问题