java 正则 replace 忽略大小写

        String description = model.getDescription();
        if (!"".equals(description)) {
            //replace(/\<img/gi,   '<img class="rich-img" ' );
            //description = description.replaceAll("<img", "<img class=\"rich-img\" ");
            //item.setDescription(description);
            
            Pattern pattern = Pattern.compile("<img", Pattern.CASE_INSENSITIVE);  
            Matcher matcher = pattern.matcher(description);  
            String result = matcher.replaceAll("<img class=\"rich-img\" ");
            model.setDescription(result);
            
            //Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);  
            //Matcher m = p.matcher(input);  
            //String result = m.replaceAll(replacement); 
        }

在做小程序时,遇到富文本中包含图片标签,图片超出了屏幕。在网上找了下资料,需要在后端处理,加个class,然后前端再针对这个class定义样式。刚好用到了正则替换,特记录下。

另外处理富文本内容,微笑小程序是这么处理的:

<rich-text nodes="{{product.description}}"></rich-text>
使用rick-text标签,然后加上nodes属性。
 
顺便提供一段代码,把<img>标签里面的其它东东全部删掉,只保留src属性
content = content.replaceAll("<img (.*) src=\"(.*)\" .* />", "<img src=\"$2\" />");

 

转载于:https://www.cnblogs.com/subendong/p/10836788.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值