使用REGEX匹配两个HTML标记之间的所有代码 [英] Match all code between two HTML tags using REGEX

查看:221
本文介绍了使用REGEX匹配两个HTML标记之间的所有代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个贪婪的RegEx匹配来替换< / title> 之间的许多HTML文件中的所有文本< / head> (我组合了大量的JavaScript文件来减少http请求的数量)。

我使用了Google,但大部分解决方案似乎都不适合我。 (这自然会导致我认为我错了)



这是我目前的表达式,似乎并不奏效: - $ / b
$ b

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

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

这两个标签之间有多个包含各种javascript文件的例子: -
,它们在逐页基础上有所不同。

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

解决方案

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

 < /标题>((?:?!(小于/头>)[\s\S])*)< /头> 

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

 < script> var str =包含< / head>的东西 ;<脚本> 

 <! - 引用< / head>的评论 - > 

等星座。

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.

解决方案

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.

这篇关于使用REGEX匹配两个HTML标记之间的所有代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆