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