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\" />");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM