Use the regular expression of the Freemaker Replace function.
Replace (param1, param2, param3)
Param1 regular expression; param2 replaces matched characters with specified characters; param3 Mode
Param3 parameters:
Mode |
I |
R |
M |
S |
C |
F |
Replace |
Supported |
Supported |
Only combined with r |
Only combined with r |
Only combined with r |
Supported |
Mode description:
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. Standard 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. Spaces and comments are allowed in regular expressions.
Example:
<#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')}
Output result:
foo bAr XYari: foo XYr XYarif: foo XYr baarr: foo XYAr XYrri: foo XYr XYrrif: foo XYr baar
More examples:
Original article: str = 2 points for 30 yuan coupon $ {str? Replace ('\ B \ d + credit', ', 'R')} output: redeem a 30 yuan coupon