freemarker replace方法正则表达式替换字符串

问题描述:需求场景,在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标签

freemarker常用技巧---特殊字符转义

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值