We help IT Professionals succeed at work.

How to use regular expressions and CFSCRIPT to remove text

I am looking for a solution that uses CFSCRIPT to remove all text that is surrounded by '<style' and '/style>' including removing the style tags. Can someone show me how I can use something like ReReplaceNoCase to solve this.

For example: if I have text that looks like:

section a <style>here is style to remove</style>section b <style>more style to remove</style>section c

I need the function to return:
section a section b section c

Thank you.
Comment
Watch Question

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
I'm not a CFSCRIPT person, but looking at that function and what you want to accomplish I would try this.  I imagine you have the text in a variable and likely need to save the result back there or something, but that's CFSCRIPT which I don't understand, so the first line is just showing the regex to do the replaces with a hard coded input text string like your sample.  The second line assumes you have a variable, but again I don't know the exact syntax in CFSCRIPT for that.

ReReplaceNoCase("section a <style>here is style to remove</style>section b <style>more style to remove</style>section c", "<style>.+?<\/style>", "", "ALL")

MyVar = ReReplaceNoCase(MyVar, "<style>.+?<\/style>", "", "ALL")

Open in new window


»bp

Author

Commented:
Thank you! I will give this a try tonight. It looks like this will solve my problem.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
And just in case it's useful, if you explore regular expressions further, there's a nice online tool that allows some trial and error and "debugging" of regular expressions easily.  Take a look at the link below, I saved the small test I did with your data and my regex just to give you idea.  It can prove very helpful as you try and dial in a regular expression to meet a certain need.  And it gives a bit of a breakdown on the pieces of the regex.

https://regex101.com/r/ZLT2SX/1


»bp

Author

Commented:
Thank you for the link. That will be very helpful, I am not very good with regular expressions and have a lot to learn
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
Referring to the link here https://cfdocs.org/rereplace the usage appears to be
I have no way of testing this - but assuming your input string is in string then something like this (assuming that cfscript RegEx supports it.
<cfscript>
replace_string = REReplace(string, "<style[\s\S]+?<\/style>", "", "ALL")
</cfscript>

Open in new window

Author

Commented:
I have tried both suggestions above and could get neither one to work, I must be missing something.

Here is my sample code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<cfscript>
function stripHTMLCont1(str) 
{
	var strTempReplace = REReplaceNoCase(str,"'<style[\s\S]+?<\/style>'","","ALL");
	
	return strTempReplace;
}

function stripHTMLCont2(str) 
{
	var strTempReplace = REReplaceNoCase(str,"'<style>.+?<\/style>'","","ALL");
	
	return strTempReplace;
}
</cfscript>

	<cfset strTestString = "begin <style type='text/css'> .fontred {color:red; } </style><span class='fontred'>here is </span><strong>text</strong> more text <i>test italic</i> end of text    <style>STYLE TO REMOVE</style> END OF STYLE">
	<cfoutput>
		Original String:<br />#strTestString#<br /><br />
	</cfoutput>

	<br /><strong>Test 1</strong><br />
	<cfset dbgString = stripHTMLCont1 (strTestString)>
	<br />
	<cfoutput>Output:<br />#dbgString#<br /><br />encodeForHTML: #encodeForHTML(dbgString)#<br /></cfoutput>

	<br /><strong>Test 2</strong><br />
	<cfset dbgString = stripHTMLCont2 (strTestString)>
	<br />
	<cfoutput>Output:<br />#dbgString#<br /><br />encodeForHTML: #encodeForHTML(dbgString)#<br /></cfoutput>
</body>
</html>

Open in new window


And here is my result:
reg_expr.PNG
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
It looks like you need to remove the single quotes from the regular expression, not sure where they came from.  So, change this:

var strTempReplace = REReplaceNoCase(str,"'<style>.+?<\/style>'","","ALL");

Open in new window

to this:
var strTempReplace = REReplaceNoCase(str,"<style>.+?<\/style>","","ALL");

Open in new window


»bp

Author

Commented:
Thank you! I did not see those single quotes.