js字符串中修改指定字符后指定的字符

str='<p>dg<b style="color: rgb(255, 0, 0);">钢结构</b><b>v不错v吧</b>风格<b style="color: rgb(255, 0, 0);">梵蒂冈地方</b>的<span style="color: rgb(255, 0, 0);">二手单反</span></p>'

处理成
str='dg<font color="#FF0000"><b> 钢结构</b></font><b>v不错v吧</b>风格<font color="#FF0000"><b> 梵蒂冈地方</b></font>的<font color="#FF0000">二手单反</font>'


要求:
1.所有带style的b标签替换成<font color="#FF0000"><b>
2.所有style="color: rgb(255, 0, 0);"换成color="#FF0000"
3.所有span标签换成font

let contentStyle=this.content.replace(/<\/?p[^>]*>/gi,'')
                contentStyle=contentStyle.replace(/span/g, 'font')
                console.log(contentStyle)
                contentStyle=contentStyle.replace(/&lt;/g, '<')
                contentStyle=contentStyle.replace(/&gt;/g, '>')
                var reg4 = /style="[^=]*?"([(\s+\w+=)|>])/g
                var reg = /<span\s+style=[\'\"]font-size\s*:\s*\d+pt;[\'\"]>/ig;
                contentStyle=contentStyle.replace(reg4,'color="#FF0000">')
                var index=contentStyle.indexOf('<b color="#FF0000">');
                var num = 0; // 这个字符出现的次数
                while(index !== -1) {
                    console.log(index); // 打印字符串出现的位置
                    let last=contentStyle.substring(index+1,contentStyle.length);
                    last=last.replace('</b>','</b></font>')
                    let bef=contentStyle.substring(0,index+1);
                    contentStyle=bef+last
                    num++; // 每出现一次 次数加一
                    index = contentStyle.indexOf('<b color="#FF0000">',index + 1); // 从字符串出现的位置的下一位置开始继续查找
                }
                contentStyle=contentStyle.replace(/<b color="#FF0000">/g, '<font color="#FF0000"><b> ')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值