Regular expression usage of Freemaker Replace function

  • 2020-04-01 04:30:05
  • OfStack

The replace (param1, param2, param3)

Param1 regular expression; Param2 replaces the matching character with the specified character; Param3 mode

Param3 has the following parameters

model i r m s c f
replace support support And the only r combination And the only r combination And the only r combination support



Pattern explanation:

I: Case insensitive: ignores Case

F: First only. That is, replace/find/etc. Only the First occurrence of something.

R: The substring to find is a regular expression. The standard regular expressions ((link: 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, 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, Include a line terminator. By default this expression does not match line terminators.

C: fine. Only whitespace and comments in regular expressions.

Examples are as follows:


<#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 results:

Foo bAr XYar
I: foo XYr XYar
If: foo XYr baar
R: foo XYAr XYr
Ri: foo XYr XYr
Rif: foo XYr baar 

More examples:

STR = 2 points for a 30 yuan coupon

The ${STR & # 63; Replace (' \ \ b \ \ d + points', ' ', 'r')}

Output: redeem 30 yuan coupon

Ps: replace function of freemarker

Replace the string replace ;


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

Replace all ba's in s with xy rule parameters including: I, r, m, s, c, f

, I: case insensitive.
, f: only replaces the first string that appears to be replaced & PI;
・ r:   XY is a regular expression  

・ m: Multi-line mode for regular expressions. In Multi-line mode the expressions ^ and $match just after or just before, 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, Include a line terminator. By default this expression does not match line terminators.

, c: permit whitespace and comments in regular expressions.


Related articles: