使用 REGEX 匹配两个 HTML 标记之间的所有代码

发布于 2024-09-17 10:23:13 字数 404 浏览 5 评论 0原文

我正在寻找一个贪婪的正则表达式匹配来替换 之间的多个 HTML 文件中的所有文本(我已将加载 JavaScript 文件以最大限度地减少 http 请求的数量)。

我用谷歌搜索过,但大多数解决方案似乎对我不起作用。 (这自然会让我认为我错了)

这是我当前的表达式,但似乎不起作用:-

</title>([^<]*)</head>

我正在使用 Dreamweaver 8 搜索和替换。

在两个标签之间有多个包含各种 JavaScript 文件的内容,例如:- 逐页变化。

我想用一致的 CSS / JavaScript 列表替换所有页面中这两个标签之间的所有内容。

I'm looking for a greedy RegEx match to replace all text in a number of HTML files between </title> and </head> (I've combined loads of JavaScript files to minimize the number of http requests).

I've googled it but the majority of the solutions don't seem to work for me. (Which naturally leads me to assume that i'm wrong)

This is my current expression which doesn't seem to work:-

</title>([^<]*)</head>

I'm using Dreamweaver 8 search and replace.

Between the two tags there are multiple includes for various javascript files for example:-
which vary on a page by page basis.

I want to replace everything between those two tags in all pages with a consistant list of CSS / JavaScript inclues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟凡古楼 2024-09-24 10:23:13

如果 DreamWeaver 正则表达式支持前瞻断言:

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

通常的警告“不适用于使用正则表达式的 HTML,这在某些时候会失败”适用。失败的点可能是:

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

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

其他星座。

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.

捶死心动 2024-09-24 10:23:13

您是否尝试过使用类似 http://www.regextester.com/ 的内容?你可以实时测试你的正则表达式吗?

您还可以采取其他方法来“测试”您的案例。

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.

离鸿 2024-09-24 10:23:13
//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
//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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文