[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


Regular expression to replace tags

Posted on 2004-10-20
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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="http://www.someurl.com" 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")>

Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?


Accepted Solution

umbrae earned 2000 total points
ID: 12374351
       <cfhttp method="get" url="http://www.yahoo.com" 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 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