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
content = content.replaceAll("<img (.*) src=\"(.*)\" .* />", "<img src=\"$2\" />");