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

作者:jerrylsxu 时间:2023-02-03 22:46:17 

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

范例如下:


<#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是正则表达式 

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

标签:freemaker,replace,函数,正则表达式
0
投稿

猜你喜欢

  • 如何使div在任何分辩率的情况下居中

    2007-08-13 09:10:00
  • Sql Server 和 Access 操作数据库结构Sql语句小结

    2024-01-28 14:13:39
  • Finished with error:Navicat运行SQL文件报错的解决

    2024-01-28 01:40:03
  • Python深度强化学习之DQN算法原理详解

    2023-03-05 12:02:51
  • TensorFlow固化模型的实现操作

    2022-09-12 22:28:42
  • python复制文件的方法实例详解

    2021-12-22 11:43:45
  • ie7.0浏览器 兼容问题苦煞网站设计者

    2007-08-08 17:11:00
  • python flask 如何修改默认端口号的方法步骤

    2021-07-04 16:35:14
  • 使用批处理脚本自动生成并上传NuGet包(操作方法)

    2021-12-24 18:05:34
  • 详解mysql中的存储引擎

    2024-01-27 05:53:15
  • python的继承详解

    2022-12-09 14:53:10
  • SQL Server中两种修改对象所有者的方法

    2009-01-15 13:10:00
  • pytorch SummaryWriter保存日志的方法

    2023-07-24 22:07:31
  • Python基础必备之语法结构详解

    2023-12-07 05:29:06
  • JavaScript使用Range调色及透明度实例

    2024-04-16 10:36:25
  • golang中defer的关键特性示例详解

    2023-08-06 06:12:45
  • Python自动化运维和部署项目工具Fabric使用实例

    2022-06-24 12:46:58
  • Python中Permission denied的解决方案

    2023-11-09 18:42:24
  • python numpy矩阵信息说明,shape,size,dtype

    2021-02-21 17:52:47
  • 何在MySQL数据库中定义外键

    2009-12-17 12:29:00
  • asp之家 网络编程 m.aspxhome.com