简单的语法问题

时间:2010-02-17 19:19:04

标签: regex actionscript-3 actionscript flex3

首先,对不起我的noob-ness。相信我,当我说我已经rtfm'ing。我不是懒惰,我只是愚蠢(显然)。从好的方面来说,这可以在这里获得一些简单的分数。

我正在尝试使用包含特殊字符的模式进行匹配/替换,并在Flex 3应用中遇到语法错误。我只想要以下正则表达式编译...(同时也用“”替换html标签)

value.replace(/</?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)/?>/g, "");

另外,在标签之间有html实体的情况下,模式/<.*?> / g不起作用, 像这样:

<TEXTFORMAT LEADING="2">
<P ALIGN="LEFT">
<FONT FACE="Arial" SIZE="11" COLOR="#4F4A4A" LETTERSPACING="0" KERNING="0"><one</FONT>
</P>
</TEXTFORMAT><TEXTFORMAT LEADING="2">
<P ALIGN="LEFT">
<FONT FACE="Arial" SIZE="11" COLOR="#4F4A4A" LETTERSPACING="0" KERNING="0">two</FONT>
</P>
</TEXTFORMAT>

第一个正则表达式将同时获得"<one" and "two",,但第二个只能获得“hi”

谢谢! Stabby L

1 个答案:

答案 0 :(得分:1)

以下是您要找的内容:

// strips htmltags
// @param html - string to parse
// @param tags - tags to ignore
public static function stripHtmlTags(html:String, tags:String = ""):String
{
    var tagsToBeKept:Array = new Array();
    if (tags.length > 0)
        tagsToBeKept = tags.split(new RegExp("\\s*,\\s*"));

    var tagsToKeep:Array = new Array();
    for (var i:int = 0; i < tagsToBeKept.length; i++)
    {
        if (tagsToBeKept[i] != null && tagsToBeKept[i] != "")
            tagsToKeep.push(tagsToBeKept[i]);
    }

    var toBeRemoved:Array = new Array();
    var tagRegExp:RegExp = new RegExp("<([^>\\s]+)(\\s[^>]+)*>", "g");

    var foundedStrings:Array = html.match(tagRegExp);
    for (i = 0; i < foundedStrings.length; i++) 
    {
        var tagFlag:Boolean = false;
        if (tagsToKeep != null) 
        {
            for (var j:int = 0; j < tagsToKeep.length; j++)
            {
                var tmpRegExp:RegExp = new RegExp("<\/?" + tagsToKeep[j] + " ?/?>", "i");
                var tmpStr:String = foundedStrings[i] as String;
                if (tmpStr.search(tmpRegExp) != -1) 
                    tagFlag = true;
            }
        }
        if (!tagFlag)
            toBeRemoved.push(foundedStrings[i]);
    }
    for (i = 0; i < toBeRemoved.length; i++) 
    {
        var tmpRE:RegExp = new RegExp("([\+\*\$\/])","g");
        var tmpRemRE:RegExp = new RegExp((toBeRemoved[i] as String).replace(tmpRE, "\\$1"),"g");
        html = html.replace(tmpRemRE, "");
    } 
    return html;
}

有关详细信息,请参阅http://fightskillz.com/2010/01/flexactionscript-3-0-strip-html-tags-function/