您的位置:程序门 -> .net技术 -> asp.net



滤掉 html标记


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


滤掉 html标记[已结贴,结贴人:liyaoge]
发表于:2007-09-05 19:52:02 楼主
大家好!   我遇到了一个正则表达式问题    
regex   rx   =   new   regex(@ "( <\/?(?!td ¦tr ¦u ¦table ¦img ¦div ¦span ¦br ¦object ¦select ¦/select ¦option ¦/option ¦param ¦&nbsp ¦sup ¦/sup ¦sub ¦/sub ¦p ¦/p)[^> \/]*)\/?> ¦ <![^> ]+> ",   regexoptions.ignorecase);
string   result   =   rx.replace(str,   " ");
我的目的是   滤掉   html标记(除tr ¦u ¦table ¦img ¦div ¦span.....特殊标记)

遇到问题是

str= " <font> 111 <img   ondblclick=modify(this);   title=\ "wertewt <   <   <   123.gif\ "> </font> ";  

应得到结果:

111 <img   ondblclick=modify(this);   title= "wertewt <   <   <   123.gif "   >


但实际   我得到的是   111 <img   ondblclick=modify(this);   title= "wertewt
发表于:2007-09-05 20:19:441楼 得分:0
呵,居然还有一帖,看来心情挺迫切,那我现在写下吧
发表于:2007-09-05 20:34:412楼 得分:0
除tr ¦u ¦table ¦img ¦div ¦span.....特殊标记正则没见过,ding
发表于:2007-09-05 21:21:373楼 得分:70
终于搞定了,简单测试了下,楼主再测下吧,如果有不符合的,给出实例

private   string   replacetag(string   src)
{
        int   smarks   =   0;
        int   dmarks   =   0;
        int   level   =   0;

        stringbuilder   sb   =   new   stringbuilder();

        stringbuilder   temp   =   new   stringbuilder();

        list <string>   list   =   new   list <string> (new   string[]   {   "td ",   "/td ",   "tr ",   "/tr ",   "u ",   "/u ",   "table ",   "/table ",   "img ",   "div ",   "/div ",   "span ",   "/span ",   "br ",   "/br ",   "br/ ",   "object ",   "select ",   "/select ",   "option ",   "/option ",   "param ",   "&nbsp; ",   "sup ",   "/sup ",   "sub ",   "/sub ",   "p ",   "/p "   });

        foreach   (char   c   in   src)
        {
                if   (level   ==   0)
                {
                        if   (c   ==   ' < ')
                        {
                                level   =   1;
                                continue;
                        }
                        sb.append(c);
                }
                else   if   (level   ==   1)
                {
                        if   (c   ==   '   ')
                        {
                                if   (list.contains(temp.tostring().tolower()))
                                {
                                        sb.append( " < "   +   temp.tostring()   +   "   ");
                                        level   =   2;
                                }
                                else
                                        level   =   3;
                                temp   =   new   stringbuilder();
                        }
                        else   if   (c   ==   '> ')
                        {
                                if   (list.contains(temp.tostring().tolower()))
                                        sb.append( " < "   +   temp.tostring()   +   "> ");
                                temp   =   new   stringbuilder();
                                level   =   0;
                        }
                        else
                                temp.append(c);
                }
                else   if   (level   ==   2)
                {
                        if   (smarks   ==   0   &&   c   ==   '\ ' ')
                        {
                                smarks   =   1;
                        }
                        else   if   (smarks   ==   1   &&   c   ==   '\ ' ')
                        {
                                smarks   =   0;
                        }

                        if   (dmarks   ==   0   &&   c   ==   ' " ')
                        {
                                dmarks   =   1;
                        }
                        else   if   (dmarks   ==   1   &&   c   ==   ' " ')
                        {
                                dmarks   =   0;
                        }

                        if   (smarks   ==   0   &&   dmarks   ==   0)
                        {
                                if   (c   ==   '> ')
                                {
                                        level   =   0;
                                }
                        }

                        sb.append(c);
                }
                else   if   (level   ==   3)
                {
                        if   (smarks   ==   0   &&   c   ==   '\ ' ')
                        {
                                smarks   =   1;
                        }
                        else   if   (smarks   ==   1   &&   c   ==   '\ ' ')
                        {
                                smarks   =   0;
                        }

                        if   (dmarks   ==   0   &&   c   ==   ' " ')
                        {
                                dmarks   =   1;
                        }
                        else   if   (dmarks   ==   1   &&   c   ==   ' " ')
                        {
                                dmarks   =   0;
                        }

                        if   (smarks   ==   0   &&   dmarks   ==   0)
                        {
                                if   (c   ==   '> ')
                                        level   =   0;
                        }
                }
        }

        return   sb.tostring();
}


调用:
string   test   =   " <font> 111 <img   ondblclick=modify(this);   title=\ "wertewt <   <   <   123.gif\ "   align=center   alt= 'ccc < < <ddd> > eee '   > </font> ";
richtextbox2.text   =   replacetag(test);

输出:
111 <img   ondblclick=modify(this);   title= "wertewt <   <   <   123.gif "   align=center   alt= 'ccc < < <ddd> > eee '   >


正则写了一个,在测试工具里通用,但是放到程序里,可能是由于多次循环尝试匹配,导致程序崩溃,这种有限状态机的方法只循环遍历一次,比正则的效率要高出很多,就是写起来比正则要困难一些

当然,这里的list也可以做为参数,动太改变,这样就具有很好的灵活性
发表于:2007-09-05 22:47:014楼 得分:0
mark
发表于:2007-09-05 23:05:305楼 得分:0
借楼主的宝地也问一个问题,如何限制输入 <a> 这个类型的东西!帮楼主顶上去!
发表于:2007-09-05 23:09:356楼 得分:0
呵呵   顶
发表于:2007-09-06 16:43:367楼 得分:0
此贴已结,谢谢大家   了,由其是lxcnn(过客)!


快速检索

最新资讯
热门点击