问题
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.
回答1:
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.
回答2:
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.
回答3:
//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
来源:https://stackoverflow.com/questions/3618839/match-all-code-between-two-html-tags-using-regex