请教高手帮忙解决jquery问题!!!急!!!

技术宅男ing 2011-07-15 10:36:59
就是一个替换的问题,我用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个数组来进行,一个放需要替换的原文,还有替换后的文字

但是正则就是用不了
...全文
117 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
技术宅男ing 2011-07-15
  • 打赏
  • 举报
回复
还有朋友愿意指点下吗??
haiqingwang1988 2011-07-15
  • 打赏
  • 举报
回复
不懂,帮忙顶下
技术宅男ing 2011-07-15
  • 打赏
  • 举报
回复
求教刚刚两位大神!!!!
技术宅男ing 2011-07-15
  • 打赏
  • 举报
回复
还是不行啊,楼上的大哥,我给你看下全文:

$(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);

});

});


我水平有限,还在学习中、、希望大家帮帮忙!!
q107770540 2011-07-15
  • 打赏
  • 举报
回复
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,'');
打字员 2011-07-15
  • 打赏
  • 举报
回复

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));

q107770540 2011-07-15
  • 打赏
  • 举报
回复
1. /<img\s*style=(['\"\s]?)margin-bottom:-1px;\1[^>]*?src=(['\"\s]?)\/output\.php\?word=[^'\"\s]+\2[^>]*?>/gi


2. /<font[^>]*>/gi
技术宅男ing 2011-07-15
  • 打赏
  • 举报
回复
没人帮忙吗!!!!!!!!!

87,989

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧