Match all code between two HTML tags using REGEX

谁说胖子不能爱 提交于 2019-12-06 05:30:43

If DreamWeaver regex supports look-ahead assertions:

</title>((?:(?!</head>)[\s\S])*)</head>

The usual warning "don't work on HTML with regex, this will fail at some point" applies. The points where this would fail could be:

<script>var str = "something that contains </head>";<script>

or

<!-- a comment that refers to </head> -->

among other constellations.

Have you tried using something like http://www.regextester.com/ ? You could test your regex live?

There are other approaches you could take to 'test' your case.

//if i consider you are using php as you are looking for regex
//regex : '#\<\title\>(.+?)\<\/head\>#s'
//in php

    $content_processed = preg_replace_callback(
      '#\<\title\>(.+?)\<\/head\>#s',
      create_function(
        '$matches',
        'return htmlentities($matches[1]);'
      ),
      $content
    );

// this will return content between the tag
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!