就是一个替换的问题,我用jquery框架写的。
需要替换掉文中的一些字符串,比如:<img style="margin-bottom:-1px;" src="/output.php?word=%C5%C5%C3%FB&r=83&g=83&b=83">这个字符串会在需要过滤的文中出现多次,都需要替换成我想要的文字当然还有其他需要过滤的,比如:<img style="margin-bottom:-1px;" src="/output.php?word=%C0%ED%BD%E2&r=83&g=83&b=83">也会出现多次,需要替换成我想要的文字,比如替换成:aa;还有文中有些html标记需要过滤掉,比如:<font color="red">,等等如果要用到正则,正则怎么写???我自己能做到把固定的字符串替换但是html的,我用正则没法过滤,郁闷了 guol[6] = /<font.[^/]+>/g;我用2个数组来进行,一个放需要替换的原文,还有替换后的文字但是正则就是用不了

解决方案 »

  1.   

    1. /<img\s*style=(['\"\s]?)margin-bottom:-1px;\1[^>]*?src=(['\"\s]?)\/output\.php\?word=[^'\"\s]+\2[^>]*?>/gi
    2. /<font[^>]*>/gi
      

  2.   


    var tagTemp = {
    'img': 'aa',
    'font': 'bb'
    };
    String.prototype.format = function(tag) {
    var temp = this;
    for(each in tag) {
    temp = temp.replace((new RegExp("<" + each + "[^>]*?>", 'gi')), tag[each]);
    }
    return temp;
    }
    var str = "wertwerte<img style=\"margin-bottom:-1px;\" src=\"/output.php?word=%C5%C5%C3%FB&r=83&g=83&b=83\">sdfgertqwet<font color=\"red\">wertwert";
    alert(str.format(tagTemp));
      

  3.   

    var html="你的HTML";
    html=html.replace(/<img\s*style=(['\"\s]?)margin-bottom:-1px;\1[^>]*?src=(['\"\s]?)\/output\.php\?word=[^'\"\s]+\2[^>]*?>/gi,'aa').replace(/<font[^>]*>/gi,'');
      

  4.   

    还是不行啊,楼上的大哥,我给你看下全文:$(function(){$(":input[name=submit]").click(function()
        {var $wenben =  $("#wenben");var wenben = $wenben.val();var guol  = new Array(); guol[0] = '<img style="margin-bottom:-1px;" src="/output.php?word=%C5%C5%C3%FB&r=83&g=83&b=83">'; // 排名
     guol[1] = '<img style="margin-bottom:-1px;" src="/output.php?word=%B9%D8%BC%FC%B4%CA&r=83&g=83&b=83">';  //关键字 guol[2] = '<img style="margin-bottom:-1px;" src="/output.php?word=%C3%AA%CE%C4%B1%BE&r=83&g=83&b=83">'; // 锚文本 guol[3] = '<img style="margin-bottom:-1px;" src="/output.php?word=%B0%FC%BA%AC&r=83&g=83&b=83">'; // 包含 guol[4] = '<img style="margin-bottom:-1px;" src="/output.php?word=%C0%ED%BD%E2&r=83&g=83&b=83">'; //  不理解
     guol[5] = "<br />"; guol[6] = /<font[^>]*>/gi; guol[7] = "</font>";
    var  tihuan = new Array();tihuan[0] = "排名";tihuan[1] = "关键字";tihuan[2] = "锚文本";tihuan[3] = "包含";tihuan[4] = "不理解";tihuan[5] = "";tihuan[6] = "";tihuan[7] = "";for(var i =0;i < guol.length;i++){  while(wenben.indexOf(guol[i]) != -1) {
       wenben = wenben.replace(guol[i],tihuan[i]);  }
    }
     $("#result").html(wenben);});});
    我水平有限,还在学习中、、希望大家帮帮忙!!