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

replace(param1,param2,param3)

param1 正则表达式;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.标准正则表达式(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.

(0)

相关推荐

  • [asp]中的正则表达式运用代码

    今天在改一套b2b的电子商务系统时,发现ASP正则,虽然用ASP很久了,但从来没用过正则表达式,这套系统中有个正则的函数很不错,自己看吧. 复制代码 代码如下: <%   Function RegExpTest(patrn, strng)      Dim regEx, Match, Matches   ' 建立变量.      Set regEx = New RegExp   ' 建立正则表达式.       regEx.Pattern = patrn   ' 设置模式.      regEx

  • 运用正则表达式匹配所有表名

    正则表达式,又称正规表示法.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串.在很多文本编辑器里,正则表达式通常被用来检索.替换那些符合某个模式的文本. 写出匹配SQL语句中的所有表名,备忘记录 折磨了好久,正则表达式如下: 复制代码 代码如下: \*\s+from\s+[\w\[\]]*\.?[\w\[\]]*\.?\[?(\b\w+)\]?[\r\

  • 正则表达式初运用之认证界面的实现代码

    先给大家展示下效果图: 关键代码如下所示: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Examples</title> <meta name="d

  • js编写trim()函数及正则表达式的运用

    1.js中本身是没有trim函数的,不过你可以自己写一个 复制代码 代码如下: function trim(str) { var newStr = str.replace(/^\s*$/g,'') retrun newStr; } 2.去除字符串左右两端的空格,在vbscript里面可以轻松地使用 trim.ltrim 或 rtrim,但在js中却没有这3个内置方法,需要手工编写.下面的实现方法是用到了正则表达式,效率不错,并把这三个方法加入String对象的内置方法中去. 写成类的方法格式如下

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

    replace(param1,param2,param3) param1 正则表达式: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 somethi

  • javascript中基于replace函数的正则表达式语法

    示例代码如下: var strM = "javascript is a good script language"; //在此我想将字母a替换成字母A alert(strM.replace("a","A")); [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 它只替换了首字母.但如果加上正则表达式结果就不一样了!replace()支持正则表达式,它可以按照正则表达式的规则匹配字符或字符串,然后给予替换! 注意:被替换的部分不用加双引号

  • js正则表达式之replace函数用法

    正则表达式replace()函数: 此函数用指定的字符串替换字符串中与正则表达式匹配的子字符串. 返回值是一个替换后的新字符串. 这里只介绍正则表达式的相关操作,其他替换操作可以参阅javascript的String对象的replace()方法一文. 语法结构: stringObject.replace(regexp,replacement) 参数列表如下: 参数名称 语义解释 regexp 必需.RegExp对象. replacement 必需.一个字符串值.规定了替换文本或生成替换文本的函数

  • Js 利用正则表达式和replace函数获取string中所有被匹配到的文本(推荐)

    js的replace函数除了替换文本以外还有获取所有被正则表达式匹配到的文本的功能.这里以一个简单的案例来作为演示. 利用正则查找出所有被两个花括号包裹的字符串: var str = <div class="item">{{test}}{{aaa}}{{bbb}}</div> str.replace(reg,function (match,param,offset,string) { console.log(match,param,offset,string);

  • Replace中的正则表达式

    replace:把原有的字符替换成新的字符 1. replace的字符串替换 var str = 'pku2016pku2017'; str = str.replace('pku', 'pkusoft'); console.log(str); // pkusoft2016pku2017 在不使用正则的情况下,每次执行只能替换一个字符,每次执行都是从0开始替换,有重复的,无法全部替换 2. replace的正则的替换 str = str.replace(/pku/g, 'pkusoft'); //

  • JavaScript String.replace函数参数实例说明

    Email:longsu2010 at yeah dot net js String的replace函数的函数签名如下: replace(match/* 字符串OR正则表达式 */, replacement/* 字符串OR函数 */) 作用是将源自符串中的match替换为replacement并返回替换后的字符串. 如果第一参数是字符串就没什么好说的了,但是要记住此时只在源自符串替换一次match(第一次)函数就执行完成了. 所以第一参数通常是一个正则表达式,举例如下: replace(/a/g

  • 也说JavaScript中String类的replace函数

    对回调函数的参数说明也很准确: 第一个参数是匹配到的字符串,最后一个是原字符串,倒数第二个参数是匹配到的字符串的在原字符串索引的起始位. 但我很好奇,第二到倒数第三之间的参数又是些什么呢?其实,W3school已经给出了答案: 复制代码 代码如下: replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串.其语法为: stringObject.replace(regexp/substr,replacement) replacement 可以是字符串,也可以

  • javascript中的replace函数(带注释demo)

    javascript这门语言一直就像一位带着面纱的美女,总是看不清,摸不透,一直专注服务器端,也从来没有特别重视过,直到最近几年,javascript越来越重要,越来越通用.最近和前端走的比较近,借此机会,好好巩固一下相关知识点. 1.初识replace 在js中有两个replace函数 一个是location.replace(url) 跳转到一个新的url 一个string.replace("xx","yy") 替换字符串 返回一个新的字符串,该方法并不改变字符串

  • Python3 replace()函数使用方法

    描述 replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次. 语法 replace()方法语法: str.replace(old, new[, max]) 参数 old -- 将被替换的子字符串. new -- 新字符串,用于替换old子字符串. max -- 可选字符串, 替换不超过 max 次 返回值 返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则

  • MongoDB中数据的替换方法实现类Replace()函数功能详解

    近日接到一个开发需求,因业务调整,需要DBA协助,将MongoDB数据库中某集合的进行替换.例如我们需要将集合A中B字段中,有关<美好>的字符替换为 <非常美好>.个人感觉这个需求如果是在SQL Server 或MySQL 数据库上处理是小菜一碟,如果是针对MongoDB数据,可能要费神了. 1.常见关系数据数据库中的替换函数 在SQL Server数据库中,我们用Replace函数来实现字符的替换. 语法 REPLACE ( ''string_replace1'' , ''str

随机推荐