Freemaker Replace函数的正则表达式运用

<#assign map = result>
            <#list map?keys as key>
              <#assign mapKey = map[key]>
                <#if mapKey?index_of("M")==-1 && mapKey?index_of("Y")==-1> 
                <tr class=b>
          <td>${map[key]}</td>
          <td>${key?replace('./','','r')?replace(>','','r')} </td>//替换多个
          <td></td>
             </tr>
          </#if>
 
         </#list>

Freemarker模板中字符串替换多个字符

#多个字符在后面续加 ?replace 即可

${params.data? replace('&lt;','<')? replace('&gt;','>')}

replace(param1,param2,param3)

param1 正则表达式;param2 将匹配的字符替换成指定字符;param3 模式

param3 参数如下

 

模式irmscf
replace支持支持只和r 组合只和r 组合只和r 组合支持

 

 

 



模式解释:

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.在正则表达式中允许空格和注释。

范例如下:

?

1

2

3

4

5

6

7

<#assign s = 'foo bAr baar'>

${s?replace('ba', 'XY')}

i: ${s?replace('ba', 'XY', 'i')}

if: ${s?replace('ba', 'XY', 'if')}

r: ${s?replace('ba*', 'XY', 'r')}

ri: ${s?replace('ba*', 'XY', 'ri')}

rif: ${s?replace('ba*', 'XY', 'rif')}

输出结果:

foo bAr XYar
i: foo XYr XYar
if: foo XYr baar
r: foo XYAr XYr
ri: foo XYr XYr
rif: foo XYr baar 

更多范例:

原文:str = 2积分兑换30元优惠券

${str?replace('\\b\\d+积分','','r')}

输出:兑换30元优惠券

ps:freemarker的replace功能

替换字符串 replace    

?

1

2

${s?replace(‘ba', ‘XY' )}

${s?replace(‘ba', ‘XY' , ‘规则参数')}

将s里的所有的ba替换成xy 规则参数包含: i r m s c f 具体含义如下: 

· i: 大小写不区分.  
· f: 只替换第一个出现被替换字符串的字符串  
· r:  XY是正则表达式 

· 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.  

· s: Enables dotall mode for regular expressions (same as Perl singe-line mode). In dotall 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. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值