Regular expression for nested tags (innermost to make it easier)

前端 未结 4 867

I researched this quite a bit, but couldn\'t find a working example how to match nested html tags with attributes. I know it is possible to match balanced/neste

4条回答
  •  没有蜡笔的小新
    2021-01-23 02:26

    Matching innermost matching pairs of

    &
    tags, plus their attributes & content:

    #)).)*

    #s

    The key here is that (?:(?!STRING).)* is to strings as [^CHAR]* is to characters.

    Credit: https://stackoverflow.com/a/6996274


    Example in PHP:

    
      in 1
      
    in 2
    in 3
in 4
in 5
EOD; $matches = array(); preg_match_all('#)).)*
#s', $text, $matches); foreach ($matches[0] as $index => $match) { echo "************" . "\n" . $match . "\n"; }

Outputs:

************
in 3
************
in 5

提交回复
热议问题