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定義樣式。恰好用到了正則替換,特記錄下。前端

另外處理富文本內容,微笑小程序是這麼處理的:node

<rich-text nodes="{{product.description}}"></rich-text>
使用rick-text標籤,而後加上nodes屬性。
 
順便提供一段代碼,把<img>標籤裏面的其它東東所有刪掉,只保留src屬性
content = content.replaceAll("<img (.*) src=\"(.*)\" .* />", "<img src=\"$2\" />");