问题描述:需求场景,在freemarker页面显示富文本带有回车符的字符串,要求回车符处进行换行
一般js可以使用replace方法a.replace("param1","param2","param3")
param1 正则表达式;param2 将匹配的字符替换成指定字符;param3 模式
模式解释:
i: Case insensitive: 忽略大小写
f: First only. That is, replace/find/etc. only the first occurrence of something.
r: The substring to find is a regular expression.标准正则表达式(http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html )
m: Multi-line mode for regular expressions. In multi-line mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the string. By default these expressions only match at the beginning and the end of the entire string. Note that ^ and $ doesn't match the line-break character itself.
s: Enables dot-all mode for regular expressions (same as Perl singe-line mode). In dot-all mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.
c: Permits whitespace and comments in regular expressions.在正则表达式中允许空格和注释。
具体参考的博客链接如下:https://blog.csdn.net/MarkArch/article/details/51281800
用了文章中的replace的实例,报错了
${s?replace(/\n|\r\n/g,"<br>")} 这是我们公司前端在别处js用的 /\n|\r\n/g该正则为会出符 将回车符替换成<br>标签
又尝试第一个参数加引号 ${s?replace("/\n|\r\n/g","<br>")}也不行 还是报错。
正确写法为:${s?replace("\r\n","<br>")},难道是freemarker对正则不需要转义么,百度也没看到关于这方面的论述,记录下来,防止下次遇到同样的坑,总之这样确实能讲字符串中带有回车符的替换成br标签