Hawks
Hawks

Reputation: 534

Remove font tags from htmlText

I'd like to remove the font tags from the htmlText string produced by a TextField leaving the surrounding nodes and any bold etc tags within intact.

Example partial output of htmlText:

<P ALIGN="LEFT"><FONT FACE="ChampagneLimo" SIZE="18" COLOR="#000000" LETTERSPACING="0" KERNING="0">Lorem Ipsum</FONT></P>

My plan was to avoid trying anything with regex and create an XML object.

However if I create a new XML object containing a root node and then attempt to appendChild the htmlText string so that I have a valid XML object to manipulate I run into a problem with html entities, see example below:

<html>&lt;P ALIGN="LEFT"&gt;&lt;FONT FACE="...

How can the font tags be stripped from htmlText and how can I create a valid XML object from the htmlText string? My plan was to use the XML replace() method but I am open to suggestions.

Upvotes: 1

Views: 1954

Answers (1)

weltraumpirat
weltraumpirat

Reputation: 22604

Just add the <html> tag to the string instead of creating an extra node:

var xml : XML = new XML ("<html>"+ myTextField.htmlText + "</html>");

You could also use a regular expression to remove the font tags:

var reg:RegExp = /\<\/?FONT.*?\/?\>/gi; 
// matches all <FONT> start and end tags 
// (case-insensitive), along with any attributes 
var myHtmlText:String = myTextField.htmlText.replace (reg, "");

Upvotes: 3

Related Questions