使用正则表达式去除 Flex/AS3 中的 HTML 标签 [英] Use regular expressions to remove HTML tags in Flex/AS3

查看:21
本文介绍了使用正则表达式去除 Flex/AS3 中的 HTML 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Flex (AS3) 中编写一个 HTML 解析器,我需要删除一些不需要的 HTML 标签.

例如,我想从这段代码中删除 div:

 

<div><div><div><div><div><div><p style="padding-left: 18px; padding-right: 20px; text-align: center;"><span></span><span style=" font-size: 48px; color: #666666; font-style: normal; font-weight: bold; text-decoration: none; font-family: Arial;">20% 折扣.</跨度><跨度></span><span style=" font-size: 48px; color: #666666; font-style: normal; font-weight: normal; text-decoration: none; font-family: Arial;">现在做吧!</跨度><跨度></span></p>

并以这样的方式结束:

 

<p style="padding-left: 18px; padding-right: 20px; text-align: center;"><span></span><span style=" font-size: 48px; color: #666666; font-style: normal; font-weight: bold; text-decoration: none; font-family: Arial;">20% 折扣.</跨度><跨度></span><span style=" font-size: 48px; color: #666666; font-style: normal; font-weight: normal; text-decoration: none; font-family: Arial;">现在做吧!</跨度><跨度></span></p>

我的问题是,如何编写正则表达式来删除这些不需要的 DIV?有没有更好的方法?

提前致谢.

解决方案

假设您的目标 HTML 实际上是有效的 XML,您可以使用递归函数来拖出非 div 位.

静态函数grabNonDivContents(xml:XML):XMLList {var out:XMLList = new XMLList();var kids:XMLList = xml.children();对于每个(var Kid:XML in kids){if (kid.name() &&kid.name() == "div") {var grandkids:XMLList =grabNonDivContents(kid);对于每个(var grandkid:grandkids中的XML){出 += 大孩子;}} 别的 {出 += 孩子;}}返回;}

I'm writing a HTML parser in Flex (AS3) and I need to remove some HTML tags that are not needed.

For example, I want to remove the divs from this code:

           <div>
              <div>
                <div>
                  <div>
                    <div>
                      <div>
                        <div>
                          <p style="padding-left: 18px; padding-right: 20px; text-align: center;">
                            <span></span>
                            <span style=" font-size: 48px; color: #666666; font-style: normal; font-weight: bold; text-decoration: none; font-family: Arial;">20% OFF.</span>
                            <span> </span>
                            <span style=" font-size: 48px; color: #666666; font-style: normal; font-weight: normal; text-decoration: none; font-family: Arial;">Do it NOW!</span>
                            <span> </span>
                          </p>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>

and end with something like this:

                      <div>
                          <p style="padding-left: 18px; padding-right: 20px; text-align: center;">
                            <span></span>
                            <span style=" font-size: 48px; color: #666666; font-style: normal; font-weight: bold; text-decoration: none; font-family: Arial;">20% OFF.</span>
                            <span> </span>
                            <span style=" font-size: 48px; color: #666666; font-style: normal; font-weight: normal; text-decoration: none; font-family: Arial;">Do it NOW!</span>
                            <span> </span>
                          </p>
                        </div>

My question is, how can I write a regular expression to remove these unwanted DIVs? Is there a better way to do it?

Thanks in advance.

解决方案

Assuming that your target HTML is actually valid XML, you can use a recursive function to drag out the non-div bits.

static function grabNonDivContents(xml:XML):XMLList {
    var out:XMLList = new XMLList();
    var kids:XMLList = xml.children();
    for each (var kid:XML in kids) {
        if (kid.name() && kid.name() == "div") {
            var grandkids:XMLList = grabNonDivContents(kid);
            for each (var grandkid:XML in grandkids) {
                out += grandKid;
            }
        } else {
            out += kid;
        }
    }
    return out;
}

这篇关于使用正则表达式去除 Flex/AS3 中的 HTML 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆