Check out "Do you speak JavaScript?" - my latest video course on advanced JavaScript.
Language APIs, Popular Concepts, Design Patterns, Advanced Techniques In the Browser

Remove tags from a string in AS3

A couple of months ago I integrated a nice WYSIWYG editor written in Flex in my CMS. It worked fine, till yesterday I needed to add more features like text formatting and font color changing. As you know the Flex RichTextEditor component adds some additional tags that I definitely don't want to send to the database. So I wrote a simple function to remove these tags.

public function removeTagsFromString(text: String, tags: Array = null): String {
    if (text.length == 0) {
      return text;
    }
    if (tags == null) {
      var removeHTML: RegExp = new RegExp("<[^>]*>", "gi");
      text = text.replace(removeHTML, "");
    } else {
      var numOfTags: int = tags.length;
      for (var i: int = 0; i < numOfTags; i++) {
        var tag: String = tags[i];
        removeHTML = new RegExp("<" + tag + "[^>]*>", "gi");
        text = text.replace(removeHTML, "");
        removeHTML = new RegExp("</" + tag + "[^>]*>", "gi");
        text = text.replace(removeHTML, "");
      }
    }
    return text;

And a way of how I use it:

var str:String = _editor.htmlText;
str = removeTagsFromString(str, ["TEXTFORMAT", "FONT"]);
If you enjoy this post, share it on Twitter, Facebook or LinkedIn.