replace(param1,param2,param3)
param1 Regex;param2 將匹配的字元替換成指定字元;param3 模式
param3 參數如下
| 模式 |
i |
r |
m |
s |
c |
f |
| 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.標準Regex(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.在Regex中允許空格和注釋。
範例如下:
<#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
${s?replace(‘ba', ‘XY' )} ${s?replace(‘ba', ‘XY' , ‘規則參數')}
將s裡的所有的ba替換成xy 規則參數包含: i r m s c f 具體含義如下:
· i: 大小寫不區分.
· f: 只替換第一個出現被替換字串的字串
· r: XY是Regex
· 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.