Regular expression to replace tags

Posted on 2004-10-20
Last Modified: 2013-12-24
Does anyone have a regular expression that will remove any tag and the content between the tags and replace it with the blank tag value? For instance if I want to remove the "<head></head>" tags and any character and/or nested tags between them and replace them with "<head><title>My Title</title></head>".
Question by:McHack
LVL 35

Expert Comment

ID: 12362885
You could do it in two steps.

Step 1) Replace <head>....</head> with <head></head>

You could use a regular expression similar to:

Step 2) Then you can replace <head></head> with what you want using a simple replace.

You could probably combine into one step, up to you.

Author Comment

ID: 12363580
Ok, that didn't work. When I ran that example the page "<HEAD></HEAD>" tags were not removed and the content in between them was not removed of which there is a ton (javascripts, meta tags etc.).

Here is the code I was running:

       <cfhttp method="get" url="" resolveurl="yes">
            <CFSET ThrowError = true>
<cfset ApScriptEdit = #ReReplaceNoCase(cfhttp.FileContent, "<head>*</head>", "", "ALL")#>
LVL 10

Expert Comment

ID: 12366895

Almost there try this:

<cfset ApScriptEdit  = rereplacenocase(cfhttp.FileContent, "<head>.*?</head>","","ALL")>

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.


Accepted Solution

umbrae earned 500 total points
ID: 12374351
       <cfhttp method="get" url="" resolveurl="yes">
            <CFSET ThrowError = true>
<cfset ApScriptEdit =
"<head[^>]*>.*</head[^>]*>","<head><title>My Title</title></head>","ALL")>

in order to catch all of the possibilities I'd probably using something like this. It'd catch if they have anything inside the head (standard html does not, but they may have a space or something like that).

Just my 2 cents.

Expert Comment

ID: 12374358
Whoops. Probably want to remove the htmlCodeFormat() from that output, was doing that for debugging.


Author Comment

ID: 12376494

Works great, just what I was looking for.

It looks so simple when I see the solution but some how I never seem to get the regular expressions right.

Thanks for the help!


Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Forbidden errors 5 156
Using random iterations in password hashing.  Good or Bad? 4 93
JKS to store upstart data 2 116
DNS, website, godaddy 6 94
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question