0

I'm using Coldfusion.
The following syntax seems to remove all HTML tags for the str variable:

ReReplaceNoCase(#str#,"<[^>]*(?:>|$)","","ALL")>

However, I'd like to keep both <div> and </div> intact. How can I do that?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
user963063
  • 73
  • 1
  • 10
  • possible dublicate of http://stackoverflow.com/questions/970817/how-can-i-clean-html-tags-out-of-a-coldfusion-string – yash Jul 11 '16 at 11:14

1 Answers1

0

Instead of a regex, I would recommend using JSoup. It makes parsing and manipulating html fragments much easier.

Download and install JSoup. Create a Whitelist with the tags you wish to keep. Then scrub your html string with JSoup.clean(...):

jsoup = createObject("java", "org.jsoup.Jsoup");
whiteList = createObject("java", "org.jsoup.safety.Whitelist");
cleanString = jsoup.clean( yourHTMLString, Whitelist.none().addTags( [ "div" ] ));
writeDump( cleanString );
Leigh
  • 28,765
  • 10
  • 55
  • 103