Replacing tags in braces, even nested tags, with regex

前端 未结 3 1129
感情败类
感情败类 2021-01-26 08:36

Example

preg_replace(\'/\\{[a-zA-Z.,\\(\\)0-9]+\\}/\', \'Replaced\', \'Lorem ipsum dolor sit {tag1({tag2()})}, consectetur adipiscing elit.\');
         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-26 09:22

    You need to add curly braces to your character set. Here's the pattern I used:

    /\{[a-zA-Z.,\(\)\{\}0-9]+\}/
    

    And here was the result:

    "Lorem ipsum dolor sit Replaced, consectetur adipiscing elit."
    

提交回复
热议问题